mmoreram/symfony-bundle-dependencies
Composer 安装命令:
composer require mmoreram/symfony-bundle-dependencies
包简介
Dependencies resolver for Symfony Bundles
关键字:
README 文档
README
The minimum requirements of this bundle is PHP 7.1 and Symfony 3.2 because the bundle is using features on both versions. If you're not using them yet, I encourage you to do it.
This package provides a very simple way of adding dependencies between Symfony Bundles. Composer defines these definitions in a very soft layer, only downloading these dependent packages. Bundles should as well force other Bundles to be instanced in the application to comply with Dependency Injection dependencies.
For your Bundle
If you want your bundles to provide this feature, then is as simple as make your bundles implement an interface. That simple.
Take in account that this addition will only provide compatibility with projects using this project, and will not affect anyway projects not using it.
use Symfony\Component\HttpKernel\Bundle\Bundle; use Mmoreram\SymfonyBundleDependencies\DependentBundleInterface; /** * My Bundle */ class MyBundle extends Bundle implements DependentBundleInterface { /** * Create instance of current bundle, and return dependent bundle namespaces * * @return array Bundle instances */ public static function getBundleDependencies(KernelInterface $kernel) { return [ 'Another\Bundle\AnotherBundle', 'My\Great\Bundle\MyGreatBundle', // ... ]; } }
Maybe one of your bundle dependencies need an specific value in the constructor. Well, this is a very very weird case, and you should definitely avoid it, but you can do it by adding the instance instead of the namespace.
use Symfony\Component\HttpKernel\Bundle\Bundle; use Mmoreram\SymfonyBundleDependencies\DependentBundleInterface; /** * My Bundle */ class MyBundle extends Bundle implements DependentBundleInterface { /** * Create instance of current bundle, and return dependent bundle namespaces * * @return array Bundle instances */ public static function getBundleDependencies(KernelInterface $kernel) { return [ 'Another\Bundle\AnotherBundle', 'My\Great\Bundle\MyGreatBundle', new \Even\Another\Bundle\EvenAnotherBundle('some-value'), ]; } }
By default, all bundles defined as their namespace are instanced with the kernel object as first parameter, so doing something like that doesn't really have sense at all.
use Mmoreram\SymfonyBundleDependencies\DependentBundleInterface; /** * My Bundle */ class MyBundle implements DependentBundleInterface { /** * Create instance of current bundle, and return dependent bundle namespaces * * @return array Bundle instances */ public static function getBundleDependencies(KernelInterface $kernel) { return [ new \Even\Another\Bundle\EvenAnotherBundle($this), ]; } }
As you will see later, using instances instead of names will remove the possibility of using cache in the final project.
For your Kernel
In your project, you should be able to resolve all these dependencies. This is why this package offers you as well a way of doing that in your kernel.
use Symfony\Component\HttpKernel\Kernel; use Mmoreram\SymfonyBundleDependencies\BundleDependenciesResolver; /** * Class AppKernel */ class AppKernel extends Kernel { use BundleDependenciesResolver; /** * Register application bundles * * @return array Array of bundles instances */ public function registerBundles() { return $this->getBundleInstances([ '\My\Bundle\MyBundle', ]); } }
In that case, you can pass as well instances of bundles instead of strings.
use Symfony\Component\HttpKernel\Kernel; use Mmoreram\SymfonyBundleDependencies\BundleDependenciesResolver; /** * Class AppKernel */ class AppKernel extends Kernel { use BundleDependenciesResolver; /** * Register application bundles * * @return array Array of bundles instances */ public function registerBundles() { return $this->getBundleInstances([ new \My\Bundle\MyBundle($this), ]); } }
Performance
As you may see, resolving dependencies can penalize a lot your website performance. Each time your Kernel is booted, all dependencies are resolved once and again, and this has no sense at all.
This package offers you as well a cache layer, reducing to 0 from the second time your Kernel is booted and until your next deployment (cache file is stored in Kernel cache folder).
One simple change to your code. That easy.
use Mmoreram\SymfonyBundleDependencies\CachedBundleDependenciesResolver; /** * Class AppKernel */ class AppKernel { use CachedBundleDependenciesResolver; /** * Register application bundles * * @return array Array of bundles instances */ public function registerBundles() { return $this->getBundleInstances([ '\My\Bundle\MyBundle', ]); } }
Caching your bundle dependencies resolution can only be used when all dependencies are defined as strings instead of instances.
This library assumes that, as soon as something changes in your project that can change the dependencies file, you will remove cache. Just take it in account.
The order
Of course, the order matters. If two of your dependencies instantiate the same bundle with different parameters, then the first one to be defined will be the winner. In that case, if you want to explicitly define how a bundle must be instantiated even if other dependencies do, add this bundle at the beginning of your array.
use Symfony\Component\HttpKernel\Kernel; use Mmoreram\SymfonyBundleDependencies\BundleDependenciesResolver; /** * Class AppKernel */ class AppKernel extends Kernel { use BundleDependenciesResolver; /** * Register application bundles * * @return array Array of bundles instances */ public function registerBundles() { return $this->getBundleInstances([ new \My\Bundle\MyBundle($this, true), 'Another\Bundle\AnotherBundle', 'My\Great\Bundle\MyGreatBundle', ]); } }
This is also applied when defining the bundle dependencies.
mmoreram/symfony-bundle-dependencies 适用场景与选型建议
mmoreram/symfony-bundle-dependencies 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 194.73k 次下载、GitHub Stars 达 57, 最近一次更新时间为 2015 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「dependencies」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mmoreram/symfony-bundle-dependencies 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mmoreram/symfony-bundle-dependencies 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mmoreram/symfony-bundle-dependencies 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dependency graph visualization for composer.json
g4 dependencies injection container based on Pimple
Provides Function To Check if a plugin is active/inactive & function to compare versions.
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
统计信息
- 总下载量: 194.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 57
- 点击次数: 17
- 依赖项目数: 53
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-23