本文整理汇总了PHP中Phalcon\DI::getShared方法的典型用法代码示例。如果您正苦于以下问题:PHP DI::getShared方法的具体用法?PHP DI::getShared怎么用?PHP DI::getShared使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\DI
的用法示例。
在下文中一共展示了DI::getShared方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get
/**
* {@inheritdoc}
*/
public function get($id)
{
if (!$this->has($id)) {
throw new Exception\ServiceNotFound("Service `{$id}` not found in the container.");
}
try {
return $this->di->getShared($id);
} catch (DI\Exception $e) {
throw new Exception\ContainerError("Cannot retrieve `{$id}` from the container.", 0, $e);
}
}
示例2: registerServices
/**
* @param DI $di
*/
public function registerServices($di)
{
//Registering a dispatcher
$di->setShared('dispatcher', function () use($di) {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Admin");
$eventsManager = $di->getShared('eventsManager');
$eventsManager->attach('dispatch', new AdminSecurity($di));
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
$auto_admin = new \AutoAdmin\Module();
$auto_admin->registerServices($di);
$di->setShared('admin_views_dir', function () {
return ADMINROOT . '/views/';
});
$di->setShared('session', function () {
$session = new Session();
$session->start();
return $session;
});
$di->setShared('config', function () use($di) {
$configFront = (require COREROOT . '/app/config/config.php');
$configBack = (require ADMINROOT . '/config/config.php');
$configFront->merge($configBack);
return $configFront;
});
}
示例3: registerServices
/**
* Registration services for specific module
* @param \Phalcon\DI $di
* @access public
* @return mixed
*/
public function registerServices($di)
{
// Dispatch register
$di->set('dispatcher', function () use($di) {
$eventsManager = $di->getShared('eventsManager');
$eventsManager->attach('dispatch:beforeException', new \Plugins\Dispatcher\NotFoundPlugin());
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
$dispatcher->setDefaultNamespace('Modules\\' . self::MODULE . '\\Controllers');
return $dispatcher;
}, true);
// Registration of component representations (Views)
$di->set('view', function () {
$view = new View();
$view->setViewsDir($this->_config['application']['viewsBack'])->setMainView('auth-layout')->setPartialsDir('partials');
return $view;
});
require_once APP_PATH . '/Modules/' . self::MODULE . '/config/services.php';
// call profiler
if ($this->_config->database->profiler === true) {
new \Plugins\Debugger\Develop($di);
}
if (APPLICATION_ENV == 'development') {
// share Fabfuel topbar
$profiler = new \Fabfuel\Prophiler\Profiler();
$di->setShared('profiler', $profiler);
$pluginManager = new \Fabfuel\Prophiler\Plugin\Manager\Phalcon($profiler);
$pluginManager->register();
// add toolbar in your basic BaseController
}
return;
}
示例4: testProfilersMustBeExecutedBeforeSerialization
/**
* @depends testProfilerManagerAttachesDataCollectorsToVariousEvents
*/
public function testProfilersMustBeExecutedBeforeSerialization()
{
$profiler = $this->di->get(ProfilerDataCollector::DI_NAME);
$eventsManager = $this->di->getShared('eventsManager');
$profiler->enable($this->getDataCollectors());
try {
json_encode($profiler);
$this->fail();
} catch (\Exception $e) {
do {
$e = $e->getPrevious();
} while ($e && $e->getPrevious());
$this->assertInstanceOf('\\Vegas\\Profiler\\Exception\\EventNotTriggeredException', $e);
}
$eventsManager->fire('db:beforeQuery', $this->di->getShared('db'));
$this->di->getShared('db')->execute('SELECT 1');
$eventsManager->fire('db:afterQuery', $this->di->getShared('db'));
$eventsManager->fire('dispatch:beforeException', $this->di->getShared('dispatcher'), new \Vegas\Exception());
$eventsManager->fire('dispatch:afterDispatchLoop', $this->di->getShared('dispatcher'));
$eventsManager->fire('view:afterRender', $this->di->getShared('view'));
$result = json_encode($profiler);
$this->assertJson($result);
}
示例5: getConnection
/**
* Return the config object in the services container
*
* @return \Phalcon\Mvc\Url
*/
public static function getConnection()
{
return self::$di->getShared('db');
}
示例6: attachView
/**
* @param $view
*
* @throws \DebugBar\DebugBarException
*/
public function attachView($view)
{
if (!$this->shouldCollect('view', true) || isset($this->collectors['views'])) {
return;
}
// You can add only One View instance
if (is_string($view)) {
$view = $this->di[$view];
}
$engins = $view->getRegisteredEngines();
if (isset($engins['.volt'])) {
$volt = $engins['.volt'];
if (is_object($volt)) {
if ($volt instanceof \Closure) {
$volt = $volt($view, $this->di);
}
} elseif (is_string($volt)) {
if (class_exists($volt)) {
$volt = new Volt($view, $this->di);
} elseif ($this->di->has($volt)) {
$volt = $this->di->getShared($volt, array($view, $this->di));
}
}
$engins['.volt'] = $volt;
$view->registerEngines($engins);
$volt->getCompiler()->addExtension(new VoltFunctions($this->di));
}
$viewProfiler = new Registry();
$viewProfiler->templates = array();
$viewProfiler->engines = $view->getRegisteredEngines();
$config = $this->config;
$eventsManager = $view->getEventsManager();
if (!is_object($eventsManager)) {
$eventsManager = new Manager();
}
$eventsManager->attach('view:beforeRender', function ($event, $view) use($viewProfiler) {
$viewProfiler->startRender = microtime(true);
});
$eventsManager->attach('view:afterRender', function ($event, $view) use($viewProfiler, $config) {
$viewProfiler->stopRender = microtime(true);
if ($config->options->views->get('data', false)) {
$viewProfiler->params = $view->getParamsToView();
} else {
$viewProfiler->params = null;
}
});
$eventsManager->attach('view:beforeRenderView', function ($event, $view) use($viewProfiler) {
$viewFilePath = $view->getActiveRenderPath();
if (Version::getId() >= 2000140) {
if (!$view instanceof \Phalcon\Mvc\ViewInterface && $view instanceof \Phalcon\Mvc\ViewBaseInterface) {
$viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
}
} elseif ($view instanceof Simple) {
$viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
}
$templates = $viewProfiler->templates;
$templates[$viewFilePath]['startTime'] = microtime(true);
$viewProfiler->templates = $templates;
});
$eventsManager->attach('view:afterRenderView', function ($event, $view) use($viewProfiler) {
$viewFilePath = $view->getActiveRenderPath();
if (Version::getId() >= 2000140) {
if (!$view instanceof \Phalcon\Mvc\ViewInterface && $view instanceof \Phalcon\Mvc\ViewBaseInterface) {
$viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
}
} elseif ($view instanceof Simple) {
$viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
}
$templates = $viewProfiler->templates;
$templates[$viewFilePath]['stopTime'] = microtime(true);
$viewProfiler->templates = $templates;
});
$view->setEventsManager($eventsManager);
$collector = new ViewCollector($viewProfiler, $view);
$this->addCollector($collector);
}