DI容器 - 正确的方法吗?

时间:2015-04-09 07:24:46

标签: php symfony inversion-of-control ioc-container

我有很长的依赖注入列表来显示带有文章,导航等的页面。目前我把它们放在一个名为index.php的文件中,将它们粘合在一起。

的index.php,

use MyCustomVerndorName\Constant\Mapper\ConstantsMapper;
use MyCustomVerndorName\Slug\Mapper\SlugMapper;
.... (more)

$ConstantService = new ConstantService();
$ConstantController = new ConstantController();

$ArticleService = new ArticleService();
$ArticleController = new ArticleController();

// Prepare Nav model.
$NavModel = new NavModel();
$NavMapper = new NavMapper($PdoAdapter);
$NavService->setMapper($NavMapper)->setModel($NavModel);

// Prepare Article model.
$ArticleModel = new ArticleModel();
$ArticleMapper = new ArticleMapper($PdoAdapter);

// Prepare components.
$ArticleContentComponent = new ArticleContentComponent($PdoAdapter);
... (more)

// Inject components.
$ArticleMapper->addComponent($ArticleContentComponent);
... (more)

$NavChildrenComponent = new NavChildrenComponent($PdoAdapter);
... (more)

// Inject components.
$NavMapper->addComponent($NavChildrenComponent);
$NavMapper->addComponent($NavLanguageComponent);

// Controll the slug.
$SlugController->setService($SlugService)->fetchRow([
    "url"   =>  $url
]);

// Control the nav.
$NavController->setService($NavService)->fetchRows();

// Controll the article.
$ArticleService->setMapper($ArticleMapper)->setModel($ArticleModel);
$ArticleController->setService($ArticleService)->fetchRow([
    "url"   =>  $url
]);

// Prepare template.
$PageTemplate = new PageTemplate();

// Prepare view.
$ArticleView = new ArticleView($PageTemplate, $ArticleModel);
$ArticleView->setSlug($SlugModel);
$ArticleView->setNav($NavModel);

// Render the view.
echo $ArticleView->render('index.php');

在我的router.php中(我正在使用AltoRouter),

use AltoRouter as Router;

$Router = new Router();.
$Router->map('GET', '/', '/article/container');
... (and other routes)

if($match)
{
    $target = $match['target'];
    $url = isset($match['params']['url']) ? $match['params']['url'] : DEFAULT_HOMEPAGE;
    $language = isset($match['params']['language']) ? $match['params']['language'] : null;

    include __DIR__ . $target . '.php';

}

我正在考虑将index.php中的依赖注入列表放到容器类中,这样我就可以在需要时调用这个类,

对于router.php中的instace,

$Router->map('GET', '/', 'MyCustomVerndorName\Article\Container\ArticleContainer');
....
new $target($PdoAdapter, $url, $language);

这个容器类,

namespace MyCustomVerndorName\Article\Container;

// Mapper.
use MyCustomVerndorName\Constant\Mapper\ConstantsMapper;
...

class ArticleContainer
{

    /*
     * Construct dependency.
     */ 
    public function __construct(\MyCustomVerndorName\Adapter\PdoAdapter $PdoAdapter, $url, $language)
    {
        $ConstantService = new ConstantService();
        $ConstantController = new ConstantController();

        // Slug.
        $SlugService = new SlugService();
        $SlugController = new SlugController();

        // Nav.
        $NavService = new NavService();
        $NavController = new NavController();

        // Article.
        $ArticleService = new ArticleService();
        $ArticleController = new ArticleController();

        // Prepare Article model.
        $ArticleModel = new ArticleModel();
        $ArticleMapper = new ArticleMapper($PdoAdapter);

        // Prepare components.
        $ArticleContentComponent = new ArticleContentComponent($PdoAdapter);
        ...

        // Inject components.
        $ArticleMapper->addComponent($ArticleContentComponent);
        ...

        // Control the nav.
        $NavController->setService($NavService)->fetchRows();

        // Controll the article.
        $ArticleService->setMapper($ArticleMapper)->setModel($ArticleModel);
        $ArticleController->setService($ArticleService)->fetchRow([
            "url"   =>  $url
        ]);


        // Prepare template.
        $PageTemplate = new PageTemplate();

        // Prepare view.
        $ArticleView = new ArticleView($PageTemplate, $ArticleModel);
        $ArticleView->setSlug($SlugModel);
        $ArticleView->setNav($NavModel);

        // Render the view.
        echo $ArticleView->render('index.php');
    }
}

基本上,我将所有依赖关系列表放入__construct

public function __construct(\MyCustomVerndorName\Adapter\PdoAdapter $PdoAdapter, $url, $language)
{
...
}

那么,这是正确的 DI容器的方式,而不依赖于PHP的众所周知的容器(例如Pimple,Symfony \ DependencyInjection等)?

1 个答案:

答案 0 :(得分:1)

传统上,DI容器会根据需要返回服务。您所谓的ArticleContainer实际上只是一个函数,然后呈现一个视图。它会进行大量的DI注射,但它不是容器。

假设您有一个真正的容器都已正确初始化。您的代码如下:

$container = new Container();

// Some initialization

// Now use it
$viewArticle = $container->get('view_article');
echo $viewArticle->render('index.php');

希望你能看出差异。初始化完成后,您的应用程序只需提取所需的视图并呈现它。无需担心pdo或url等。

那么我们如何将文章查看服务纳入容器?

$container = new Container();

// Some initialization
$container->set('page_template',function($container)
{
  return new PageTemplate();
}
$container->set('article_view',function($container)
{
  $articleView = new ArticleView(
    $container->get('page_template', 
    $container->get('article_model')
  );
  $articleView->setSlug($container->get('slug_model');
  $articleView->setNav ($container->get('nav_model');

  return $articleView;
};
// Now use it
$viewArticle = $container->get('article_view');
echo $viewArticle->render('index.php');

因此我们为容器定义了两个服务函数。当我们执行$ container-> get('article_view');时,容器以容器本身作为参数调用函数。该函数通过从容器中提取依赖项来创建所需的类,然后返回新对象。

假设您的应用程序中可能有更多页面视图,并且大多数页面视图需要page_template。我们定义的相同page_template服务也可以由您的其他视图使用。所以我们开始从容器中重新使用。

您基本上只是继续向容器添加服务。

您可以使用Pimple,但制作自己的容器也很有帮助。真的不多。