LoginSignup
0
0

【覚書き】PHPフレームワーク「Flow」におけるインターフェースをDIしたときの挙動について

Last updated at Posted at 2023-12-12

初めに

最近PHPのマイナーフレームワークFlowを触っているのですが、インターフェースをDIしたときの挙動について気になることがありました。
いろいろ調べてみたので分かったことをアウトプットします。

DIしたインターフェースのメソッドを呼び出すとどうなる?

Flowのソースを見てると、以下のようにDIしたインターフェースのメソッドを呼び出すような処理がありました。


class Hoge {

    /**
     * @Flow\Inject
     * @var \Neos\Welcome\Service\Interface\FugaInterface
     */
    protected $fugaInterface;

    public function hogeMethod()
    {
        $this->fugaInterface->fugaMethod()
    }
}

最終的にfugaMethod()を呼び出していますが、$fugaInterfaceはインターフェースのため実装は書いてありません。
このように、実装クラスを明示していない場合、どのメソッドが呼び出されるのでしょうか?

インターフェースをDIしたときの挙動

Flowの公式ドキュメントにはインターフェースをDIしたときの挙動について以下のように書いてあります。

All PHP interfaces for which only one implementation class exist are also automatically registered as object names, with the implementation class being returned when asked for an instance of the interface.

↓GPTに要約してもらいました。

この文は、PHPにおいて、単一の実装クラスしか存在しないインターフェースは自動的にオブジェクト名として登録され、そのインターフェースのインスタンスが要求されると、対応する実装クラスが返されると述べています。

つまり、インターフェースの実装クラスが1つの場合、FWが実装クラスを自動で選択してくれるというわけですね。

試してみた

というわけで、実装クラスが1つの場合と複数ある場合をそれぞれ試してみました。

パターン1:実装クラスが1つの場合

以下のようなディレクトリ構成にしました。
ControllerでインターフェースをDIしてメソッドを呼び出すシンプルな作りです。

Packages/
  ├ Application/
  |    └ Neos.Welcome/
  |         └ Classes/
  |              ├ Controller/
  |              |    └ TestExceptionHandlerController.php(★)
  |              └ Service/
  |                   ├ Impl/
  |                   |    └ TestImpl.php(★)
  |                   └ Interface/
  |                        └ TestInterface.php(★)
  ├ Framework/
  └ Libraries/

それぞれのソースは以下になります。

Controller
<?php
namespace Neos\Welcome\Controller;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;

class DiTestController extends ActionController
{

    /**
     * @Flow\Inject
     * @var \Neos\Flow\Mvc\View\JsonView
     */
    protected $view;

    /**
     * @Flow\Inject
     * @var \Neos\Welcome\Service\Interface\TestInterface
     */
    protected $testInterface;

    /**
     * @return void
     */
    public function interfaceTestAction()
    {
        $this->view->assign('value', array(
            $this->testInterface->testMethod()
        ));
    }
}
Interface
<?php
namespace Neos\Welcome\Service\Interface;

interface TestInterface
{

    /**
     * @return void
     */
    public function testMethod();
}
Impl
<?php
namespace Neos\Welcome\Service\Impl;

use \Neos\Welcome\Service\Interface\TestInterface;

class TestImpl implements TestInterface
{

    /**
     * @return void
     */
    public function testMethod()
    {
        return "output testImpl !!";
    }
}

実際に叩いてみたところ、実装クラスで定義したメソッドが呼び出されていることが確認できました。

$ curl http://localhost:8081/Neos.Welcome/DiTest/interfaceTest
["output testImpl !!"]

パターン2:実装クラスが複数の場合

以下のようにTestImterfaceを継承するクラスを2つに増やしました。

Packages/
  ├ Application/
  |    └ Neos.Welcome/
  |         └ Classes/
  |              ├ Controller/
  |              |    └ TestExceptionHandlerController.php(★)
  |              └ Service/
  |                   ├ Impl/
  |                   |    ├ TestImpl.php(★)
  |                   |    └ TestImpl2.php(★)
  |                   └ Interface/
  |                        └ TestInterface.php(★)
  ├ Framework/
  └ Libraries/

キャッシュをクリアしてサーバを立て直したところ、TestInterfaceが見つからないという旨のメッセージがでて失敗しました。

$ ./flow server:run
The object "Neos\Welcome\Service\Interface\TestInterface" which was specified as a property in the object configuration of object "Neos\Welcome\Controller\DiTestController" (automatically registered class) does not exist.
Check for spelling mistakes and if that dependency is correctly
configured.

最後に

DIを自動で解決してくれるのは便利ですが、知らないと混乱しそうですね。
(自分が知らないだけで他のFWもこうなのかもしれないですが)
また一つFlowについて知ることができました。
また何か分かったらアウトプットしようと思います。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0