Giter Site home page Giter Site logo

symbok-bundle's Introduction

โš ๏ธ Status: Archived

With PHP 8.1 and the emergence of public readonly properties, this bundle repository has been archived and is no longer maintained.

If you really need to generate getters and setters generation, you can have a look at lombok-php.

Symbok Annotation Bundle

Packagist GitHub Actions Status

Runtime code generator bundle for Symfony.

  • Detects classes that are using Symbok annotations, generates related methods and loads generated class instead of the original one.
  • Stores generated classes in Symfony cache so that Symbok compiles them just once.
  • Reads basic Doctrine annotations to handle property's type, nullable status and entity relation.

Initially inspired by Plumbok.

Compatible with Symfony 4 and 5

Symbok ?

๐Ÿ‘‹ Bye bye endless PHP classes !

Symbok provides annotations in order to generate on the fly predictable and repetitive methods.

Available annotations are:

  • AllArgsConstructor
  • Data
  • ToString
  • Getter
  • Setter
  • Nullable

Symbok also parses doctrine properties annotations such as Column, JoinColumn, OneToOne, OneToMany, ManyToOne, ManyToMany in order to automatically discover property type, nullable status and adapt generated methods.

You'll be able to find more precise information on Symbok Bundle in the documentation

Getting started

Installation

You can easily install Symbok by composer

$ composer require mtarld/symbok-bundle

Then, bundle should be registered. Just verify that config\bundles.php is containing :

Mtarld\SymbokBundle\SymbokBundle::class => ['all' => true]

Configuration

Once Symbok is installed, you should configure it to fit your needs.

To do so, edit config/packages/symbok.yaml

# config/packages/symbok.yaml

symbok:
    # Namespaces that you wanna be processed
    namespaces:
        - 'App\Entity'
        - 'App\Model'
        
    defaults:
        getter: ~
            # If getters are nullable by default (default true)
            nullable: ~

        setter: ~
            # If setters are fluent by default (default true)
            fluent: ~

            # If setters are nullable by default (default true)
            nullable: ~

            # If setters should update other side when relation is detected (default true)
            updateOtherSide: ~

        constructor:
            # If constructor uses nullable parameters (default true)
            nullable: ~

And you're ready to go ! ๐Ÿš€

Basic example

Register your namespace in config file

# config/packages/symbok.yaml

symbok:
    namespaces:
      - 'App\Entity'

Then edit your class by adding annotations

<?php

// src/Entity/Product.php

namespace App\Entity;

use Mtarld\SymbokBundle\Annotation\Getter;

class Product
{
    /**
     * @Getter
     */
    private int $id;
}

Then, the class will be executed as the following:

<?php

namespace App\Entity;

use Mtarld\SymbokBundle\Annotation\Getter;

class Product
{
    /**
     * @Getter
     */
    private int $id;
    
    public function getId(): ?int
    {
        return $this->id;
    }
}

Provided commands

Updating original files with symbok:update:classes

$ php bin/console symbok:update:classes

When running this command, original classes' docblock will be updated with good @method tags so that IDEs will be able to know that new methods exist.

For instance, the class:

<?php

// src/Entity/Product.php

namespace App\Entity;

use Mtarld\SymbokBundle\Annotation\Getter;

class Product
{
    /**
     * @var int
     * @Getter
     */
    private $id;
}

Will be rewritten as:

<?php

// src/Entity/Product.php

namespace App\Entity;

use Mtarld\SymbokBundle\Annotation\Getter;

/**
 * @method int getId()
 */
class Product
{
    /**
     * @var int
     * @Getter
     */
    private $id;
}

Previewing results with symbok:preview

$ php bin/console symbok:preview [-s|--compilationStrategy COMPILATIONSTRATEGY] <class path>

By using that command, you will be able preview Symbok compilation results directly in your CLI.

Compilation strategy represents which compilation will be applied on target class. It could be either:

  • runtime to preview PHP code that will be executed at runtime
  • saved to preview PHP code that will be written when using symbok:update:classes command

Documentation

A detailed documentation is available here

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

After writing your fix/feature, you can run following commands to make sure that everyting is still ok.

# Install dev dependencies
$ composer install

# Running tests locally
$ make test

Authors

  • Mathias Arlaud - mtarld - <mathias(dot)arlaud@gmail(dot)com>

symbok-bundle's People

Contributors

mtarld avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

carlobeltrame

symbok-bundle's Issues

Add general configuration

Add general configuration using config yaml symfony files.
Such as 'are properties nullable by default' or 'are setters fluent by default ?'

Compiling Fails on nested Annotations

Example Docblock

/**
 * Customer
 *
 * @RecordChangeLoggable
 * @ApiResource
 * @ORM\Table(name="customers", indexes={@ORM\Index(name="salesRepEmployeeNumber", columns={"salesRepEmployeeNumber"})})
 * @ORM\Entity
 */

Stacktrace (json)
scratch_1.txt

I think its because it gets translated to this :

/**
 * Customer
 *
 * @Surplex\PhpComponent\RecordChangeLoggerBundle\Components\Annotations\RecordChangeLoggable
 * @ApiPlatform\Core\Annotation\ApiResource
 * @Doctrine\ORM\Mapping\Table (name="customers", indexes={@ORM\Index (name="salesRepEmployeeNumber", columns={"salesRepEmployeeNumber"})})
 * @Doctrine\ORM\Mapping\Entity
 */

The nested @ORM\Index does not get replaced for the FQDN

Bundle version 2.x causes @Vich\Uploadable annotation on entity to be removed

I tried installing the newest dev version to solve the issues I encountered with Symfony4, unfortunately it seems to be not compatible with Vich\UploaderBundle.

My Entity has the Vich\UploaderBundle\Mapping\Annotation\Uploadable annotation on the class as is required by the other bundle but after installing symbok the annotation seems to be removed:

The class "App\Entity\Advertisement" is not uploadable. If you use annotations to configure VichUploaderBundle, you probably just forgot to add `@Vich\Uploadable` on top of your entity. If you don't use annotations, check that the configuration files are in the right place. In both cases, clearing the cache can also solve the issue.

My class looks like this:

/**
 * @ORM\Entity(repositoryClass="App\Repository\AdvertisementRepository")
 * @Data(fluent=true)
 * @Vich\Uploadable()
 */
class Advertisement
{
...properties here
}

PHP 8 support

Does this bundle support PHP 8? I was able to install it and use the basic functionality using --ignore-platform-reqs, but it would be nice if the bundle could be specified to support PHP 8.

Usage of static analysis tools

Hi,

Has this bundle been tested by anyone using static analysis tools like phan and phpstan ?
I was just wondering because we use this and would hate to find out at a later stage that this bundle would break the analysis procedure.

Thanks in advance!

Bundle is not compatible with Symfony 4.4

The Bundle breaks with symfony 4.4 because the AutoloadService tries to use Symfony\Component\Debug\DebugClassLoader

which was replaced by Symfony\Component\ErrorHandler\DebugClassLoader in Symfony 4.4

I think the best way to fix this is to check for both for now until the old class is removed to provide backwards compatibility.

Interfaces skip

Hi, i found a problem when using interfaces in the same namespace as model namespace given for symbook. Example:

App\DataForm\Domain\Model\FormTemplate -> implements FormTemplateInterface
App\DataForm\Domain\Model\FormTemplateInterface

with configuration:

symbok:
    namespaces:
        - 'App\DataForm\Domain\Model'

Treats interface as class instance even if it not annotated with symbok. I get exception:

Mtarld\SymbokBundle\Exception\CodeFindingException: No class found

vendor/mtarld/symbok-bundle/src/Finder/PhpCodeFinder.php:94
vendor/mtarld/symbok-bundle/src/Factory/ClassFactory.php:56
vendor/mtarld/symbok-bundle/src/Compiler/RuntimeClassCompiler.php:44
vendor/mtarld/symbok-bundle/src/Replacer/RuntimeClassReplacer.php:51
vendor/mtarld/symbok-bundle/src/Replacer/RuntimeClassReplacer.php:41
vendor/mtarld/symbok-bundle/src/Cache/RuntimeClassCache.php:31
vendor/symfony/config/ConfigCacheFactory.php:46
vendor/mtarld/symbok-bundle/src/Cache/RuntimeClassCache.php:32
vendor/mtarld/symbok-bundle/src/Autoload/Autoloader.php:67
vendor/symfony/error-handler/DebugClassLoader.php:347
var/cache/test/symbok/App/DataForm/Domain/Model/FormTemplate.php:15
vendor/mtarld/symbok-bundle/src/Autoload/Autoloader.php:68
vendor/symfony/error-handler/DebugClassLoader.php:347
tests/DataForm/Domain/Model/FormTemplateTest.php:15

If i remove FormTemplateInterface completly error is gone.

Non-standard global annotations make compilation failing

When we use global annotations from third party library such as @SuppressWarnings from PHPMD,
this causes following error on compilation:

[Semantical Error] The annotation "@App\Entity\SuppressWarnings" in  does not exist, or could not be auto-loaded.

It's because src/Parser/DocBlockParser/Formatter.php is wrongly prepending Symbok's namespace.

Should be fixed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.