neighborhoods / dependency-injection-container-builder
A dependency injection container builder based on Symfony.
Installs: 8 309
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 1
Requires
- php: ^7.3|^8
- psr/container: ^1.0
- symfony/config: ^4
- symfony/dependency-injection: ^4
- symfony/finder: ^4
- symfony/yaml: ^4
Requires (Dev)
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^9.3
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2024-11-29 06:35:28 UTC
README
Basic example of usage
$container = (new \Neighborhoods\DependencyInjectionContainerBuilderComponent\TinyContainerBuilder()) ->setContainerBuilder(new \Symfony\Component\DependencyInjection\ContainerBuilder()) ->setRootPath(dirname(__DIR__)) ->addSourcePath('src/ComponentName') ->addSourcePath('src/Prefab5') ->addSourcePath('fab/ComponentName') ->makePublic(SomeRepository::class) ->addCompilerPass(new \Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass()) ->addCompilerPass(new \Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass()) ->build();
setContainerBuilder
: the setter takes instance of\Symfony\Component\DependencyInjection\ContainerBuilder
. It's possible to supply non-empty container.setRootPath
: takes the path to the project root (wheresrc
andfab
folders are located)addSourcePath
: takes the path to a folder containing definitions for Container BuildermakePublic
: takes the service URI (usually class name) and makes it publicmakeAllPublic
: makes all services publicaddCompilerPass
: takes instance of\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface
and supplies it to theaddCompilerPass
method of ContainerBuilder.build
: creates and returns an instance of\Psr\Container\ContainerInterface
With Cache
If container needs to be cached, a \Neighborhoods\DependencyInjectionContainerBuilderComponent\CacheHandlerInterface
can be supplied through setCacheHandler
.
$cacheHandler = (new \Neighborhoods\DependencyInjectionContainerBuilderComponent\SymfonyConfigCacheHandler\Builder()) ->setName('ContainerName') ->setCacheDirPath(dirname(__DIR__) . '/data/cache') ->setDebug(true) ->build(); $container = (new \Neighborhoods\DependencyInjectionContainerBuilderComponent\TinyContainerBuilder()) // ... ->setCacheHandler($cacheHandler) ->build();
setName
: takes the name of the cached Container classsetCacheDirPath
: takes the path to directory where container file is going to be stored (absolute)setDebug
: takes a boolean flag whether "debug mode" should be on or off. When debug mode is on, cache is going to "listen" for the changes in source configuration files. If any change is introduced, cache would be considered invalid, and a new one will be generated and stored. It's advised to usetrue
only for development.