symplify / autodiscovery
Forget manual registration of Doctrine entities, Twig templates and routes. Let autodiscovery handle that for you.
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Requires
- php: >=7.3
- nette/utils: ^3.0
- symfony/dependency-injection: ^5.1
- symfony/finder: ^4.4|^5.1
- symfony/routing: ^4.4|^5.1
- symplify/package-builder: ^9.0.34
- symplify/smart-file-system: ^9.0.34
- symplify/symplify-kernel: ^9.0.34
Requires (Dev)
- doctrine/cache: ^1.10
- doctrine/common: ^3.1
- doctrine/doctrine-bundle: ^2.2
- doctrine/orm: ^2.7
- doctrine/persistence: ^2.1
- phpunit/phpunit: ^9.5
- symfony/doctrine-bridge: ^4.4|^5.1
- symfony/framework-bundle: ^4.4|^5.1
- symfony/http-kernel: ^4.4|^5.1
- symfony/templating: ^4.4|^5.1
- symfony/translation: ^4.4|^5.1
- symfony/twig-bundle: ^4.4|^5.1
- symplify/easy-testing: ^9.0.34
- dev-master / 9.1.x-dev
- 9.0.34
- 9.0.33
- 9.0.32
- 9.0.31
- 9.0.30
- 9.0.29
- 9.0.28
- 9.0.27
- 9.0.26
- 9.0.25
- 9.0.24
- 9.0.23
- 9.0.22
- 9.0.21
- 9.0.20
- 9.0.19
- 9.0.18
- 9.0.17
- 9.0.16
- 9.0.15
- 9.0.14
- 9.0.13
- 9.0.12
- 9.0.11
- 9.0.10
- 9.0.9
- 9.0.8
- 9.0.7
- 9.0.6
- 9.0.5
- 9.0.4
- 9.0.3
- 9.0.2
- 9.0.1
- 9.0.0
- 9.0.0-rc1
- 9.0.0-BETA9
- 9.0.0-BETA8
- 9.0.0-BETA7
- 9.0.0-BETA6
- 9.0.0-BETA5
- 9.0.0-BETA4
- 9.0.0-BETA3
- 9.0.0-BETA2
- 9.0.0-BETA1
- 8.3.48
- 8.3.47
- 8.3.46
- 8.3.45
- 8.3.44
- 8.3.43
- 8.3.42
- 8.3.41
- 8.3.40
- 8.3.39
- 8.3.38
- 8.3.37
- 8.3.36
- 8.3.35
- 8.3.34
- 8.3.33
- 8.3.32
- 8.3.31
- 8.3.30
- 8.3.29
- 8.3.28
- 8.3.27
- 8.3.26
- 8.3.25
- 8.3.24
- 8.3.23
- 8.3.22
- 8.3.21
- 8.3.20
- 8.3.19
- 8.3.18
- 8.3.17
- 8.3.16
- 8.3.15
- 8.3.14
- 8.3.13
- 8.3.12
- 8.3.11
- 8.3.10
- 8.3.7
- 8.3.6
- 8.3.5
- 8.3.4
- 8.3.3
- 8.3.2
- 8.3.1
- 8.3.0
- 8.2.27
- 8.2.26
- 8.2.25
- 8.2.24
- 8.2.22
- 8.2.21
- 8.2.20
- 8.2.19
- 8.2.18
- 8.2.17
- v8.2.16
- v8.2.15
- v8.2.14
- v8.2.13
- v8.2.12
- v8.2.11
- v8.2.10
- v8.2.9
- v8.2.8
- v8.2.7
- v8.2.6
- v8.2.5
- v8.2.4
- v8.2.3
- v8.2.2
- v8.2.1
- v8.2.0
- v8.1.20
- v8.1.19
- v8.1.18
- v8.1.17
- v8.1.16
- v8.1.15
- v8.1.14
- v8.1.13
- v8.1.12
- v8.1.11
- v8.1.10
- v8.1.9
- v8.1.8
- v8.1.7
- v8.1.6
- v8.1.4
- v8.1.3
- v8.1.2
- v8.1.1
- v8.1.0
- v8.0.1
- v8.0.0
- v8.0.0-beta4
- v8.0.0-beta3
- v8.0.0-beta2
- v8.0.0-beta1
- v7.3.18
- v7.3.17
- v7.3.16
- v7.3.15
- v7.3.14
- v7.3.13
- v7.3.11
- v7.3.10
- v7.3.9
- v7.3.8
- v7.3.7
- v7.3.6
- v7.3.5
- v7.3.4
- v7.3.3
- v7.3.2
- v7.3.1
- v7.3.0
- v7.2.20
- v7.2.19
- v7.2.18
- v7.2.17
- v7.2.16
- v7.2.15
- v7.2.14
- v7.2.13
- v7.2.12
- v7.2.11
- v7.2.10
- v7.2.8
- v7.2.7
- v7.2.6
- v7.2.5
- v7.2.4
- v7.2.3
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.3
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.2
- v7.0.1
- v7.0.0
- v6.1.0
- v6.0.5
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.4.16
- v5.4.15
- v5.4.14
- v5.4.13
- v5.4.12
- v5.4.11
- v5.4.10
- v5.4.9
- v5.4.8
- v5.4.7
- v5.4.6
- v5.4.5
- v5.4.4
- v5.4.3
- v5.4.2
- v5.4.1
- v5.4.0
- v5.3.12
- v5.3.11
- v5.3.10
- v5.3.9
- v5.3.8
- v5.3.7
- v5.3.6
- v5.3.5
- v5.3.4
- v5.3.2
- v5.3.1
- v5.3.0
- v5.2.22
- v5.2.20
- v5.2.19
- v5.2.18
- dev-symplify7
This package is auto-updated.
Last update: 2021-01-14 18:06:42 UTC
README
For every
- new Entity namespace,
- new Twig path
- new Translation catalogue path
- and new routes files,
you need to modify your config. Why do it, when your application can do it for you? Do you autoload each Controller manually? :)
Another feature is YAML convertor - from old pre-Symfony 3.3 to new autodiscovery, autowire and autoconfigure format.
Install
composer require symplify/autodiscovery
Usage
1. Register Doctrine Annotation Mapping
When you create a new package with entities, you need to register them:
# app/config/doctrine.yml doctrine: orm: mappings: # new set for each new namespace ShopsysFrameworkBundle: type: annotation dir: '%shopsys.framework.root_dir%/src/Model' alias: ShopsysFrameworkBundle prefix: Shopsys\FrameworkBundle\Model is_bundle: false # new set for each new namespace ShopsysFrameworkBundleComponent: type: annotation dir: '%shopsys.framework.root_dir%/src/Component' alias: ShopsysFrameworkBundleComponent prefix: Shopsys\FrameworkBundle\Component is_bundle: false
It's called memory lock and it nicely opens doors for "I forgot that..." bugs.
How can we avoid that?
With Autodiscovery
# app/config/twig.yml doctrine: orm: - mappings: - # new set for each new namespace - ShopsysFrameworkBundle: - type: annotation - dir: '%shopsys.framework.root_dir%/src/Model' - alias: ShopsysFrameworkBundle - prefix: Shopsys\FrameworkBundle\Model - is_bundle: false - # new set for each new namespace - ShopsysFrameworkBundleComponent: - type: annotation - dir: '%shopsys.framework.root_dir%/src/Component' - alias: ShopsysFrameworkBundleComponent - prefix: Shopsys\FrameworkBundle\Component - is_bundle: false
namespace App; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel; use Symplify\Autodiscovery\Discovery; final class MyProjectKernel extends Kernel { use MicroKernelTrait; /** * @var Discovery */ private $discovery; public function __construct() { parent::__construct('dev', true); $this->discovery = new Discovery($this->getProjectDir()); } protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void { $this->discovery->discoverEntityMappings($containerBuilder); } }
2. Twig Paths
When you create a new package with templates, you need to register them:
# app/config/twig.yml twig: paths: # new line for each new package - "%kernel.root_dir%/../package/Product/templates" # new line for each new package - "%kernel.root_dir%/../package/Social/templates"
With Autodiscovery
# app/config/twig.yml twig: - paths: - - "%kernel.root_dir%/../package/Product/templates/views" - - "%kernel.root_dir%/../package/Social/templates/views"
namespace App; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel; final class MyProjectKernel extends Kernel { use MicroKernelTrait; // ... protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void { $this->discovery->discoverTemplates($containerBuilder); } }
3. Translation Paths
When you create a new package with translations, you need to register them:
# app/config/packages/framework.yml framework: translator: paths: # new line for each new package - "%kernel.root_dir%/../package/Product/translations" # new line for each new package - "%kernel.root_dir%/../package/Social/translations"
With Autodiscovery
# app/config/packages/framework.yml framework: translator: - paths: - - "%kernel.root_dir%/../package/Product/translations" - - "%kernel.root_dir%/../package/Social/translations"
namespace App; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel; final class MyProjectKernel extends Kernel { use MicroKernelTrait; // ... protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void { $this->discovery->discoverTranslations($containerBuilder); } }
4. Routing
# app/config/routes.yaml # new set for each new package product_annotations: resource: "../packages/Product/src/Controller/" type: "annotation" # new set for each new package social_annotations: resource: "../packages/Social/src/Controller/" type: "annotation"
With Autodiscovery
# app/config/routes.yaml -# new set for each new package -product_annotations: - resource: "../packages/Product/src/Controller/" - type: "annotation" - -# new set for each new package -social_annotations: - resource: "../packages/Social/src/Controller/" - type: "annotation"
namespace App; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Routing\RouteCollectionBuilder; final class MyProjectKernel extends Kernel { use MicroKernelTrait; protected function configureRoutes(RouteCollectionBuilder $routeCollectionBuilder): void { $this->discovery->discoverRoutes($routeCollectionBuilder); } }
This works very well with local packages or monorepo architecture.
Report Issues
In case you are experiencing a bug or want to request a new feature head over to the Symplify monorepo issue tracker
Contribute
The sources of this package are contained in the Symplify monorepo. We welcome contributions for this package on symplify/symplify.