アノテーションを利用することで、 ServiceProvider
の多くの記述を省略できるようになりました。
@Component
アノテーションを記述することで、クラスを DI コンテナに登録できます。
Repository クラス及び、 Service クラス(実装予定)に使用できます。
namespace Eccube\Repository;
uuse Eccube\Annotation\Component;
use Eccube\Application;
use Eccube\Common\Constant;
/**
* TaxRuleRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*
* @Component("eccube.repository.tax_rule")
*/
class TaxRuleRepository extends AbstractRepository
{
@Component("di.container.id")
という形式で記述します。
これにより、 ServiceProvider
での登録を省略できます。
// 以下の記述を @Component に代替可能
$app['eccube.repository.tax_rule'] = function () use ($app) {
return $app['orm.em']->getRepository('Eccube\Entity\TaxRule');
};
@Inject
アノテーションを記述することで、インスタンスをフィールドに設定できます。
Repository クラス及び、 Service クラス(実装予定)、 FormType で使用できます。
namespace Eccube\Repository;
use Eccube\Annotation\Inject;
use Eccube\Application;
class TaxRuleRepository extends AbstractRepository
{
/**
* @var \Eccube\Repository\BaseInfoRepository $BaseInfoRepository
* @Inject("eccube.repository.base_info")
*/
protected $BaseInfoRepository;
/**
* @var \Eccube\Application $app
* @Inject(Application::class)
*/
protected $app;
@Inject("di.container.id")
という形式で記述します。
Application インスタンスを設定したい場合は、例外として @Inject(Application::class)
と記述します。
これにより、 ServiceProvider
にて設定していた、セッターインジェクションを省略できます。
$app['eccube.repository.tax_rule'] = function () use ($app) {
$taxRuleRepository = $app['orm.em']->getRepository('Eccube\Entity\TaxRule');
$taxRuleRepository->setApplication($app); // この記述は @Inject に代替可能
return $taxRuleRepository;
};
ServiceProvider
での FormType の登録も省略できるようになりました。
FormType のフィールドに他クラスのインスタンスを設定したい場合は、 @Inject
アノテーションを使用します。
namespace Eccube\Form\Type\Admin;
use Eccube\Annotation\Inject;
use Eccube\Application;
class OrderType extends AbstractType
{
/**
* @var \Eccube\Application $app
* @Inject(Application::class)
*/
protected $app;
Repository の場合と同様に @Inject("di.container.id")
という形式で記述します。
Application インスタンスを設定したい場合は、例外として @Inject(Application::class)
と記述します。
// 以下の記述を省略可能
$app->extend('form.types', function ($types) use ($app) {
$types[] = new \Eccube\Form\Type\NameType($app['config']);
$types[] = new \Eccube\Form\Type\KanaType($app['config']);
....
});
-
form.type.extensions
Form/Extension/*TypeExtension
というファイル名は自動ロードの対象 -
eccube.entity.event.dispatcher
Entity\*EventListener
というファイル名は自動ロードの対象 -
eccube.queries
Eccube\Doctrine\Query
以下のクラスは自動ロードの対象 -
プラグインの orm.path, service を自動設定
-
@Template アノテーションのパスを自動設定
-
パスをキャッシュしておく仕組みが必要
-
Yaml の設定ファイルは PHP に変更する
-
全体的に
$app
をセットするのではなく、@Inject
を使用して必要なインスタンスのみをセットするようにしたい -
パフォーマンス改善のため、ServiceProvider を自動生成し、キャッシュ代わりに使用する