/** * Constructor for the controller * * @param Application $application * @param Console $console * @return ConsoleController */ public function __construct(Application $application, Console $console) { $this->application = $application; $this->services = $application->services(); $this->console = $console; if ($this->services->isAvailable('database')) { $this->config = (new \Phire\Model\Config())->getAll(); } }
/** * Prepare view * * @param string $template * @return void */ protected function prepareView($template) { $this->view = new View($this->viewPath . '/' . $template); $this->view->application_title = $this->application->config()['application_title']; if (isset($this->sess->failed)) { $this->view->failed = true; } if (isset($this->sess->expired)) { $this->view->expired = true; } if (isset($this->sess->saved)) { $this->view->saved = true; } if (isset($this->sess->removed)) { $this->view->removed = true; } if (isset($this->sess->user)) { $this->services['nav.top']->setRole($this->services['acl']->getRole($this->sess->user->role)); $this->services['nav.top']->returnFalse(true); if ($this->services->isAvailable('nav.fluid')) { $this->services['nav.fluid']->setRole($this->services['acl']->getRole($this->sess->user->role)); $this->services['nav.fluid']->returnFalse(true); } if ($this->services->isAvailable('nav.static')) { $this->services['nav.static']->setRole($this->services['acl']->getRole($this->sess->user->role)); $this->services['nav.static']->returnFalse(true); } $this->view->popNav = $this->services['nav.top']; $this->view->acl = $this->services['acl']; $this->view->user = $this->sess->user; $cookie = \Pop\Cookie\Cookie::getInstance(['path' => '/']); $this->view->windowWidth = $cookie['pop_current_width']; } }
/** * Constructor for the controller * * @param Application $application * @param Request $request * @param Response $response * @return AbstractController */ public function __construct(Application $application, Request $request, Response $response) { $this->application = $application; $this->services = $application->services(); $this->request = $request; $this->response = $response; $this->sess = $this->services['session']; $this->viewPath = __DIR__ . '/../../view'; if ($this->services->isAvailable('database')) { $this->config = (new \Phire\Model\Config())->getAll(); } }
<?php require_once '../../bootstrap.php'; use Pop\Service\Locator; try { // Load the services config via the constructor $locator = new Locator(array('config' => array('call' => 'Pop\\Config', 'params' => array(array('test' => 123), true)), 'rgb' => array('call' => 'Pop\\Color\\Rgb', 'params' => function () { return array(255, 0, 0); }), 'color' => function ($locator) { return new \Pop\Color\Color($locator->get('rgb')); })); // Services have not been loaded/instantiated yet print_r($locator); // Get the services as you need them print_r($locator->get('config')); print_r($locator->get('rgb')); print_r($locator->get('color')); // Now the services are loaded/instantiated within the locator object print_r($locator); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }
/** * Get a service * * @param string $name * @return mixed */ public function getService($name) { return $this->services->get($name); }
public function testRecursiveException() { $this->setExpectedException('Pop\\Service\\Exception'); $l = new Locator(array('service1' => function ($locator) { return $locator->get('service2'); }, 'service2' => function ($locator) { return $locator->get('service1'); })); $s = $l->get('service1'); }
<?php require_once '../../bootstrap.php'; use Pop\Service\Locator; class Foo { public function bar($val) { return new \Pop\Config(array('test' => $val)); } public static function baz($val) { return new \Pop\Config(array('test' => $val)); } } try { $locator = new Locator(); // Load the services manually via the setter $locator->set('config1', 'Foo->bar', array(123))->set('config2', 'Foo::baz', array(456)); // Get a service print_r($locator->get('config1')); print_r($locator->get('config2')); print_r($locator); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }
<?php require_once '../../bootstrap.php'; use Pop\Service\Locator; try { $locator = new Locator(); // Load the services manually via the setter $locator->set('config', 'Pop\\Config', array(array('test' => 123)))->set('color', 'Pop\\Color\\Color', array(new \Pop\Color\Rgb(255, 0, 0))); // Get a service print_r($locator->get('config')); // Remove a service $locator->remove('color'); print_r($locator); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }
<?php /** * Created by PhpStorm. * User: olubodun.akinyele * Date: 7/29/15 * Time: 6:10 AM */ use Pop\Service\Locator as Service, Pop\Db\Adapter\Mysql as Adapter; $di = new Service(); $services = ['url' => ['call' => ''], 'db' => ['call' => function () use($config) { $adapter = new Adapter(['host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'database' => $config->database->dbname]); return $adapter; }]]; /** * The URL component is used to generate all kind of urls in the application */ $di->setServices($services);