<?php include 'zf2bootstrap.php'; // must register autoloader $autoloader->registerNamespace('Foo\\Bar', __DIR__ . '/12'); $compiler = new Zend\Di\Definition\CompilerDefinition(); $compiler->addDirectory(__DIR__ . '/12/'); $compiler->compile(); $definitions = new Zend\Di\DefinitionList($compiler); $di = new Zend\Di\Di($definitions); $baz = $di->get('Foo\\Bar\\Baz'); // expression to test $works = $baz->bam instanceof Foo\Bar\Bam; // display result echo $works ? 'It works!' : 'It DOES NOT work!';
<?php $di = new Zend\Di\Di(); //trigger autoloader $a = $di->get('A'); unset($a); $t1 = microtime(true); for ($i = 0; $i < 10000; $i++) { $a = $di->get('A'); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);
namespace MovieApp { class Lister { public $finderA, $finderB; public function setFinderA(Finder $finder) { $this->finderA = $finder; } public function setFinderB(Finder $finder) { $this->finderB = $finder; } } class Finder { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->instanceManager()->setParameters('MovieApp\\Lister', array('MovieApp\\Lister::setFinderA:finder' => new MovieApp\Finder(), 'MovieApp\\Lister::setFinderB:finder' => function () { return new MovieApp\Finder(); })); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finderA instanceof MovieApp\Finder && $lister->finderB instanceof MovieApp\Finder && $lister->finderA !== $lister->finderB; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php namespace MovieApp { class Lister { public $dbFinder; public function __construct(DbFinder $dbFinder) { $this->dbFinder = $dbFinder; } } class DbFinder { public $username, $password = null; public function setCredentials($username, $password) { $this->username = $username; $this->password = $password; } } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $lister = $di->get('MovieApp\\Lister', array('username' => 'my-username', 'password' => 'my-password')); // expression to test $works = $lister->dbFinder instanceof MovieApp\DbFinder && $lister->dbFinder->username == 'my-username' && $lister->dbFinder->password == 'my-password'; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
public function getPaginator(array $paginatorOptions = array()) { $defaultPaginatorOptions = array('itemCountPerPage' => 10, 'pageRange' => 5, 'pageNumber' => 1); $dataClass = $this->getDataClass(); $count = $dataClass->getCount(); if (!$count) { return $this->paginator = null; } $dbPaginatorOptions = $dataClass->getPaginatorOptions(); $paginatorOptions = array_merge($defaultPaginatorOptions, $dbPaginatorOptions, $paginatorOptions); $count = (int) $count; $diConfig = array('instance' => array('Zend\\Paginator\\Adapter\\DbSelect' => array('parameters' => array('rowCount' => $count, 'select' => $dataClass->getSelect(), 'adapterOrSqlObject' => $dataClass->getSql())), 'Eva\\Paginator\\Paginator' => array('parameters' => array('rowCount' => $count, 'adapter' => 'Zend\\Paginator\\Adapter\\DbSelect')))); foreach ($paginatorOptions as $key => $value) { if (false === in_array($key, array('itemCountPerPage', 'pageNumber', 'pageRange'))) { continue; } $diConfig['instance']['Eva\\Paginator\\Paginator']['parameters'][$key] = $paginatorOptions[$key]; } $di = new \Zend\Di\Di(); $di->configure(new \Zend\Di\Config($diConfig)); $paginator = $di->get('Eva\\Paginator\\Paginator'); return $this->paginator = $paginator; }
{ public $finder; public function __construct(Finder $finder) { $this->finder = $finder; } } class Finder { public $source; public function __construct(Source $source) { $this->source = $source; } } class Source { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('instance' => array('MovieApp\\Finder' => array('shared' => false))))); $lister1 = $di->get('MovieApp\\Lister1'); $lister2 = $di->get('MovieApp\\Lister2'); // expression to test $works = $lister1->finder instanceof MovieApp\Finder && $lister1->finder !== $lister2->finder && $lister1->finder->source === $lister2->finder->source; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php $adapter = (include file_exists('bootstrap.php') ? 'bootstrap.php' : 'bootstrap.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('instance' => array('Zend\\Db\\Adapter\\Adapter' => array('parameters' => array('driver' => $dbconfig)))))); //var_dump($di->instanceManager()); $db = $di->get('Zend\\Db\\Adapter\\Adapter'); echo $db->platform->getName() . PHP_EOL;
class Page { public $blocks; public function addBlock(Block $block) { $this->blocks[] = $block; } } interface Block { } } namespace DifferentNs { class BlockOne implements \Application\Block { } class BlockTwo implements \Application\Block { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('definition' => array('class' => array('Application\\Page' => array('addBlock' => array('block' => array('type' => 'Application\\Block', 'required' => true))))), 'instance' => array('Application\\Page' => array('injections' => array('DifferentNs\\BlockOne', 'DifferentNs\\BlockTwo')))))); $page = $di->get('Application\\Page'); // expression to test $works = $page->blocks[0] instanceof DifferentNs\BlockOne && $page->blocks[1] instanceof DifferentNs\BlockTwo; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php namespace Foo\Bar { class Baz { public $bam; public function __construct(Bam $bam) { $this->bam = $bam; } } class Bam { public $username, $password = null; public function setCredentials($username, $password) { $this->username = $username; $this->password = $password; } } } namespace { include 'zf2bootstrap.php'; $di = new Zend\Di\Di(); $baz = $di->get('Foo\\Bar\\Baz', array('username' => 'my-username', 'password' => 'my-password')); // expression to test $works = $baz->bam instanceof Foo\Bar\Bam && $baz->bam->username == 'my-username' && $baz->bam->password == 'my-password'; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
/** * Get model class with injection (as a singleton) * * @param string $path Path * @param array $params Params * @return mixed */ public function get($path, $params = []) { $className = Mage::getConfig()->getModelClassName($path); return $this->_di->get($className, $params); }
/***************************************** * CONFIGURE LOGGER *****************************************/ $oInfoLogger = new Zend\Log\Logger(); $oExceptionLogger = new Zend\Log\Logger(); $oLogger = new Classes\Helpers\Logger($oInfoLogger, $oExceptionLogger, $aSectionConfig); $oLogger->ConfigureLogger('jobs'); /***************************************** * CONFIGURE ADAPTER *****************************************/ $aDbConfig = array('driver' => $aSectionConfig['database.adapter'], 'host' => $aSectionConfig['database.params.host'], 'username' => $aSectionConfig['database.params.username'], 'password' => $aSectionConfig['database.params.password'], 'dbname' => $aSectionConfig['database.params.dbname']); $oAdapter = new Zend\Db\Adapter\Adapter($aDbConfig); /***************************************** * CONFIGURE DIC *****************************************/ $oDi = new Zend\Di\Di(); $oDi->instanceManager()->setParameters('Classes\\Helpers\\File', array('aConfig' => $aSectionConfig)); $oDi->instanceManager()->setParameters('Classes\\Helpers\\Logger', array('oInfoLogger' => $oInfoLogger, 'oExceptionLogger' => $oExceptionLogger, 'aConfig' => $aSectionConfig)); $oDi->instanceManager()->setParameters('Classes\\Mappers\\JobItemsToMwXml', array('aConfig' => $aSectionConfig)); $oDi->instanceManager()->setParameters('Classes\\Db\\JobQueue', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\Box', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\Folio', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\Item', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\ErrorLog', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\MediaWiki', array('oAdapter' => $oAdapter)); $oMediaWikiDb = $oDi->get('Classes\\Db\\MediaWiki'); $oDi->instanceManager()->setParameters('Classes\\Mappers\\JobItemsToMwXml', array($oMediaWikiDb, 'aConfig' => $aSectionConfig)); /***************************************** * ADD TASK ABSTRACT *****************************************/ require_once 'Classes/TaskAbstract.php';
require_once 'library/Zend/Loader/StandardAutoloader.php'; $loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true)); $loader->registerNamespace('SON', 'library/SON'); $loader->register(); $definitionList = new Zend\Di\DefinitionList(array($runtime = new Zend\Di\Definition\RuntimeDefinition())); $di = new Zend\Di\Di($definitionList); //$di->instanceManager()->setParameters('SON\Db\Connection', array( // 'server' => 'localhost', // 'dbname' => 'banco', // 'user' => 'username', // 'password' => 123 //)); $di->instanceManager()->addAlias('conexao1', 'SON\\Db\\Connection', array('server' => 'localhost', 'dbname' => 'banco', 'user' => 'username', 'password' => 123)); $di->instanceManager()->addAlias('conexao2', 'SON\\Db\\Connection', array('server' => 'localhost', 'dbname' => 'banco2', 'user' => 'username', 'password' => 1234)); $conexao1 = $di->get('conexao1'); $conexao2 = $di->get('conexao2'); $di->instanceManager()->addTypePreference('SON\\Db\\Connection', 'conexao1'); $categoria = $di->get('SON\\Categoria', array('db' => 'conexao2')); #$di = new Zend\Di\Di; //$di->configure(new Zend\Di\Config(array( // 'definition' => array( // 'class' => array( // 'SON\Produto' => array( // 'setCategoria' => array('required'=>true) // ) // ) // ) //))); $di->configure(new Zend\Di\Config(array('definition' => array('class' => array('SON\\Produto' => array('addCategoria' => array('categoria' => array('type' => 'SON\\CategoriaInterface', 'required' => true))))), 'instance' => array('SON\\Produto' => array('injections' => array('SON\\Categoria', 'SON\\Category', 'SON\\Categoria')))))); $produto = $di->get('SON\\Produto');
class Mapper { public $drivers; public function addDriver(Driver $driver, $name) { $this->drivers[] = array($driver, $name); } } interface Driver { } } namespace DifferentNs { class DriverOne implements \System\Driver { } class DriverTwo implements \System\Driver { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('definition' => array('class' => array('System\\Mapper' => array('addDriver' => array('driver' => array('type' => 'System\\Driver', 'required' => true), 'name' => array('required' => true))))), 'instance' => array('System\\Mapper' => array('injections' => array('addDriver' => array(array('driver' => 'DifferentNs\\DriverOne', 'name' => 'foo'), array('driver' => 'DifferentNs\\DriverTwo', 'name' => 'bar')))))))); $mapper = $di->get('System\\Mapper'); // expression to test $works = $mapper->drivers[0][0] instanceof DifferentNs\DriverOne && $mapper->drivers[1][0] instanceof DifferentNs\DriverTwo; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php $di = new Zend\Di\Di(); //trigger autoloader for all required files $a1 = $di->get('B'); $a2 = $di->get('B'); $t1 = microtime(true); for ($i = 0; $i < 10000; $i++) { $a = $di->newinstance('B'); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);