phalette/pidic

Nette 依赖注入/容器 for Phalcon

资助包维护!
f3l1x

0.1 2015-09-27 14:02 UTC

This package is auto-updated.

Last update: 2024-09-08 07:22:17 UTC


README

Phalconist Build Status Code coverage Downloads this Month Latest stable HHVM Status

PiDiC 是 Nette\Di\Container 的适配器。

安装

$ composer require phalette/pidic:dev-master

依赖

配置

use Nette\DI\Compiler;
use Phalette\Pidic\Configurator;
use Phalette\Pidic\Environment;
use Phalette\Pidic\Extensions\PhalconDefaultsExtension;
use Phalette\Pidic\Extensions\PhalconExtension;
use Phalette\Pidic\PiDi;

$configurator = new Configurator();
$configurator->setMode(Environment::DEVELOPMENT);
$configurator->setCacheDir(__DIR__ . '/cache');
$configurator->onCompile[] = function (Compiler $compiler) {
    $compiler->addExtension('phalcon', new PhalconExtension());
    $compiler->addExtension('phalconDefaults', new PhalconDefaultsExtension());
};

$container = $configurator->createContainer();
$pidi = $container->getService('pidi');

通过工作示例学习

这是基于 官方教程

use Nette\DI\Compiler;
use Phalette\Pidic\Configurator;
use Phalette\Pidic\Environment;
use Phalette\Pidic\Extensions\PhalconDefaultsExtension;
use Phalette\Pidic\Extensions\PhalconExtension;
use Phalette\Pidic\PiDi;

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

try {

    // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
        '../app/models/'
    ))->register();

    // Create a DI
    $configurator = new Configurator();
    $configurator->setMode(Environment::DEVELOPMENT);
    $configurator->setCacheDir(__DIR__ . '/cache');
    $configurator->onCompile[] = function (Compiler $compiler) {
        $compiler->addExtension('phalcon', new PhalconExtension());
        $compiler->addExtension('phalconDefaults', new PhalconDefaultsExtension());
    };
    $container = $configurator->createContainer();
    $di = $container->getService('pidi');

    // Setup the view component
    $di->set('view', function () {
        $view = new View();
        $view->setViewsDir('../app/views/');
        return $view;
    });

    // Setup a base URI so that all generated URIs include the "tutorial" folder
    $di->set('url', function () {
        $url = new UrlProvider();
        $url->setBaseUri('/tutorial/');
        return $url;
    });

    // Handle the request
    $application = new Application($di);

    echo $application->handle()->getContent();

} catch (\Exception $e) {
     echo "PhalconException: ", $e->getMessage();
}

PhalconExtension

它设置自实例化静态 Phalcon\Di::setDefault()。任何从 Phalcon\Di\InjectionAwareInterface 扩展的对象都可以通过 $this->getDI() 访问 PiDiC

PhalconDefaultsExtension

此扩展替换 Phalcon\DI\FactoryDefault。它在容器中注册了 22 个基本服务(更多请参阅 文档)。

Phalcon\Di

PiDiC 实现 Phalcon\DiInterface,然后您可以在不进行任何更改的情况下更改 DI。

如何在 Phalcon 中使用 DI,您可以在 这里 阅读。

Nette\DI

请阅读 Nette 文档中的文章

但主要文章是