ding 与 zend 框架集成

发布于 2024-11-27 00:42:58 字数 337 浏览 0 评论 0原文

我们在工作场所使用 ZendFramework 来构建我们的网络应用程序。没关系,但它缺乏一些最好的现代实践(比如依赖注入和控制反转、aop 等)。

几个月来,我一直(独自)使用 Ding 框架作为 DI 和 AOP 的容器来进行测试。我真的很喜欢它,所以我想把它带入我们的项目中。

但如何呢?那么问题来了:如何正确地将 Ding 集成到 Zend Framework 应用程序中?考虑到 ZF 控制器不能是 bean(因为它们是直接从调度程序实例化的),如何正确地将所有依赖项注入其中?

Ps:不使用 Zend Framework 不是一个选择(至少在中期)。 PPS:有人愿意添加“ding”作为新标签吗?

We're using ZendFramework at my workplace for our webapps. It's ok, but it lacks some of the best modern practices (like dependency injection and inversion of control, aop, etc).

For a couple of months, I've been (on my own) using Ding framework as a container for DI and AOP as a test drive. I really like it, so I'd like to bring it into our projects.

But how? So there's the question: how to properly integrate Ding in Zend Framework applications? considering ZF controllers cant be beans (as they are instantiated right from the dispatcher), how to propertly inject all dependencies in them?

P.s: Not using Zend Framework is not an option (at least in the middle term).
P.P.S: Anyone care to add "ding" as a new tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-12-04 00:42:58

我很高兴丁能帮助你。
我为这个项目做出了贡献,并且还需要与 Zend Framework 应用程序集成。我使用 Zend 的应用程序资源和插件系统来实现这一点。

应用程序资源(您可以在项目之间重用)

<?php
class Application_Resource_Ding extends Zend_Application_Resource_ResourceAbstract
{

    protected $_options = array(
        'factory' => array(
            'bdef' => array(
                'xml' => array(
                    'filename' => array('beans.xml')
                ),
            ),
        ),
        'cache' => array(
            'proxy' => array('impl' => 'dummy'),
            'bdef' => array('impl' => 'dummy'),
            'beans' => array('impl' => 'dummy')
        )
    );

    public function init()
    {
        // set default config dir before mergin options (cant be set statically)
        $this->_options['factory']['bdef']['xml']['directories'] = array(APPLICATION_PATH .'/configs');

        $options = $this->getOptions();

        // parse factory properties (if set)
        if (isset($options['factory']['properties'])) {
            $options['factory']['properties'] = parse_ini_file(
               $options['factory']['properties']
           );
        }

        // change log4php_properties for log4php.properties (if exists)
        if (isset($options['log4php_properties'])) {
            $options['log4php.properties'] = $options['log4php_properties'];
            unset($options['log4php_properties']);
        }
        $properties = array(
            'ding' => $options
        );

        return Ding\Container\Impl\ContainerImpl::getInstance($properties);
    }

}

在控制器内部使用的操作助手:

<?php

class Application_Action_Helper_Ding extends Zend_Controller_Action_Helper_Abstract
{

    protected $ding = null;

    public function init()
    {
        // just once...
        if (null !== $this->ding) {
            return;
        }

        // get ding bootstrapped resource
        $bootstrap = $this->getActionController()->getInvokeArg('bootstrap');

        $ding = $bootstrap->getResource('ding');

        if (!$ding) {
            throw new Zend_Controller_Action_Exception(
                'Ding resource not bootstrapped'
            );
        }

        $this->ding = $ding;
    }

    public function getBean($bean)
    {
        return $this->ding->getBean($bean);
    }

    public function direct($bean)
    {
        return $this->getBean($bean);
    }

}

在您的 application.ini 中,您应该添加类似这样的内容(加上您需要的任何额外配置)

resources.frontController.actionHelperPaths.Application_Action_Helper = "Application/Action/Helper"

resources.ding.factory.properties = APPLICATION_PATH "/configs/ding.properties"
resources.ding.log4php_properties = APPLICATION_PATH "/configs/log4php.properties"

,然后在您的控制器中,请求一个 bean:

$service = $this->_helper->ding('someService');

希望这有帮助!

I'm glad Ding is helping you.
I contributed on this project and also needed to integrate with a Zend Framework application. I used Zend's application resources and plugin system to achieve this.

An application resource (you can reuse among projects)

<?php
class Application_Resource_Ding extends Zend_Application_Resource_ResourceAbstract
{

    protected $_options = array(
        'factory' => array(
            'bdef' => array(
                'xml' => array(
                    'filename' => array('beans.xml')
                ),
            ),
        ),
        'cache' => array(
            'proxy' => array('impl' => 'dummy'),
            'bdef' => array('impl' => 'dummy'),
            'beans' => array('impl' => 'dummy')
        )
    );

    public function init()
    {
        // set default config dir before mergin options (cant be set statically)
        $this->_options['factory']['bdef']['xml']['directories'] = array(APPLICATION_PATH .'/configs');

        $options = $this->getOptions();

        // parse factory properties (if set)
        if (isset($options['factory']['properties'])) {
            $options['factory']['properties'] = parse_ini_file(
               $options['factory']['properties']
           );
        }

        // change log4php_properties for log4php.properties (if exists)
        if (isset($options['log4php_properties'])) {
            $options['log4php.properties'] = $options['log4php_properties'];
            unset($options['log4php_properties']);
        }
        $properties = array(
            'ding' => $options
        );

        return Ding\Container\Impl\ContainerImpl::getInstance($properties);
    }

}

An action helper to use inside the controllers:

<?php

class Application_Action_Helper_Ding extends Zend_Controller_Action_Helper_Abstract
{

    protected $ding = null;

    public function init()
    {
        // just once...
        if (null !== $this->ding) {
            return;
        }

        // get ding bootstrapped resource
        $bootstrap = $this->getActionController()->getInvokeArg('bootstrap');

        $ding = $bootstrap->getResource('ding');

        if (!$ding) {
            throw new Zend_Controller_Action_Exception(
                'Ding resource not bootstrapped'
            );
        }

        $this->ding = $ding;
    }

    public function getBean($bean)
    {
        return $this->ding->getBean($bean);
    }

    public function direct($bean)
    {
        return $this->getBean($bean);
    }

}

In your application.ini you should add something like this (plus any extra configuration you need)

resources.frontController.actionHelperPaths.Application_Action_Helper = "Application/Action/Helper"

resources.ding.factory.properties = APPLICATION_PATH "/configs/ding.properties"
resources.ding.log4php_properties = APPLICATION_PATH "/configs/log4php.properties"

And then in your controllers, to request a bean:

$service = $this->_helper->ding('someService');

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文