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(); $di->instanceManager()->setParameters('MovieApp\\DbFinder', array('username' => 'my-username', 'password' => 'my-password')); $lister = $di->get('MovieApp\\Lister'); // 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!') . PHP_EOL; }
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; }
/** * Init messages decencies * * @return void */ private function _initMessages() { $this->_di->instanceManager()->addTypePreference('Oggetto_Messenger_Model_Message_Interface', 'Oggetto_Messenger_Model_Message_Xml'); }
/***************************************** * 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';
namespace MovieApp { class Lister { public $finder; public function setFinder(Finder $finder) { $this->finder = $finder; } } class Finder { public $id; public function __construct($id) { $this->id = $id; } } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->instanceManager()->setParameters('MovieApp\\Lister', array('finder' => function () { return new MovieApp\Finder('foo'); })); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finder instanceof MovieApp\Finder && $lister->finder->id === 'foo'; // 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 __construct($username, $password) { $this->username = $username; $this->password = $password; } } } namespace { include 'zf2bootstrap.php'; $di = new Zend\Di\Di(); $di->instanceManager()->setParameters('Foo\\Bar\\Bam', array('username' => 'my-username', 'password' => 'my-password')); $baz = $di->get('Foo\\Bar\\Baz'); // 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!'; }
<?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'))))));