Skip to content

Instantly share code, notes, and snippets.

@nanasess
Last active August 8, 2017 09:14
Show Gist options
  • Save nanasess/6496d0903cba8f587adf9bcc5a7dc522 to your computer and use it in GitHub Desktop.
Save nanasess/6496d0903cba8f587adf9bcc5a7dc522 to your computer and use it in GitHub Desktop.

CoC

DI

アノテーションを利用することで、 ServiceProvider の多くの記述を省略できるようになりました。

クラスを DIコンテナに登録する

@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;
};

FormType の登録

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 を自動生成し、キャッシュ代わりに使用する

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment