本文整理汇总了PHP中Phalcon\DI::get方法的典型用法代码示例。如果您正苦于以下问题:PHP DI::get方法的具体用法?PHP DI::get怎么用?PHP DI::get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\DI
的用法示例。
在下文中一共展示了DI::get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get
/**
* Resolves the service based on its configuration
*
* @param string $name
* @param array $parameters
* @throws RuntimeException
* @return mixed
*/
public static function get($name, $parameters = null)
{
if (self::$di == null) {
throw new RuntimeException('IoC container is null!');
}
return self::$di->get($name, $parameters);
}
示例2: testRegister
public function testRegister()
{
$this->testable->register();
/* @var Router $router */
$router = $this->di->get('router');
$this->assertInstanceOf('Phalcon\\Mvc\\Router', $router);
$this->assertEquals('bar', $router->getDefaultNamespace());
$this->assertEquals('foo', $router->getDefaultModule());
$this->assertNotEmpty($router->getRoutes());
$this->assertTrue(is_array($router->getRoutes()));
$this->assertCount(8, $router->getRoutes());
}
示例3: attachCache
public function attachCache($cacheService)
{
static $mode, $collector, $hasAttachd = array();
if (in_array($cacheService, $hasAttachd)) {
return;
}
$hasAttachd[] = $cacheService;
if (!$this->shouldCollect('cache', false)) {
return;
}
if (!is_string($cacheService)) {
throw new \Exception('The parameter must be a cache service name.');
}
if (!$mode) {
$mode = $this->config->options->cache->get('mode', 0);
}
if (!$collector) {
$mc = null;
if ($this->hasCollector('messages')) {
$mc = $this->getCollector('messages');
}
$collector = new CacheCollector($mode, $mc);
$this->addCollector($collector);
}
$backend = $this->di->get($cacheService);
if ($backend instanceof Multiple || $backend instanceof Backend) {
if ($this->shouldCollect('cache', false)) {
$this->di->remove($cacheService);
$self = $this;
$this->di->set($cacheService, function () use($self, $backend, $collector) {
return $self->createProxy(clone $backend, $collector);
});
}
}
}
示例4: prepare
/**
* Prepare controller.
*
* @param string|null $action Action name.
*
* @return void
*/
public function prepare($action = null)
{
$this->di = DI::getDefault();
$this->dispatcher = $this->di->get('dispatcher');
$this->cacheData = $this->di->get('cacheData');
if ($this->_widgetName !== null) {
if ($this->_widgetModule !== null) {
/** @var \Phalcon\Mvc\View $view */
$this->view = $view = $this->di->get('view');
$view->disableLevel(View::LEVEL_LAYOUT);
$view->disableLevel(View::LEVEL_MAIN_LAYOUT);
if ($action) {
$view->pick('../../' . $this->_widgetModule . '/Widget/' . $this->_widgetName . '/' . $action);
}
} else {
/** @var \Phalcon\Mvc\View $view */
$this->view = $view = $this->di->get('view');
$view->disableLevel(View::LEVEL_LAYOUT);
$view->disableLevel(View::LEVEL_MAIN_LAYOUT);
$view->setVars([], false);
if ($action) {
$relativePath = Utilities::getRelativePath($view->getViewsDir(), $this->di->get('registry')->directories->widgets);
$view->pick($relativePath . $this->_widgetName . '/' . $action);
}
}
}
// run init function
if (method_exists($this, 'initialize')) {
$this->initialize();
}
}
示例5: databaseErrorLog
/**
* 数据库操作异常
* @param \Phalcon\Mvc\Model $model
* @return bool
*/
public function databaseErrorLog($model)
{
$this->errorLog(ResultStatus::DATABASE_ERROR, '数据库异常.');
$error = '';
foreach ($model->getMessages() as $msg) {
$error .= $msg . ',';
}
$this->di->get('logger')->error('数据库异常, parameters:' . json_encode($this->parameters) . ', error: ' . $error);
return false;
}
示例6: testSession
/**
* @covers ULogin\Auth::hasSession()
*/
public function testSession()
{
// check return
$session = $this->invokeMethod($this->auth, 'hasSession', array());
$this->assertTrue($session, "[-] Session does not configured in DI");
//@todo check user data
$sessionContainer = $this->di->get('session');
$sessionContainer->set(Auth::KEY, array(1, 2, 3));
// check while service destroyed
$this->di->offsetUnset('session');
$session = $this->invokeMethod($this->auth, 'hasSession', array());
$this->assertFalse($session, "[-] Session must be destroyed");
}
示例7: __construct
public function __construct(DI $di, $aggregate = true, $formatter = 'line')
{
$this->_di = $di;
$this->_debugbar = $this->_di['debugbar'];
$this->_formatter = strtolower($formatter);
if ($di->has('log') && ($log = $di->get('log'))) {
$debugbar_loger = new Debugbar($di['debugbar']);
if ($log instanceof Adapter) {
$di->remove('log');
$multiple = new Multiple();
$multiple->push(clone $log);
$multiple->push($debugbar_loger);
/** @var DI\Service $service */
$di->set('log', $multiple);
} elseif ($log instanceof Multiple) {
$log->push($debugbar_loger);
}
$this->_aggregate = $this->isAggregate($aggregate);
}
}
示例8: testRegisterView
public function testRegisterView()
{
$view = \Mockery::mock('mirolabs\\phalcon\\Framework\\View\\View')->shouldDeferMissing()->makePartial();
$dependencyInjection = new DI();
$modulePath = 'projectPath/modules';
$moduleName = 'test';
$config = new Map();
$config->set('view', json_encode(['compiledPath' => 'compiledPath', 'compiledSeparator' => 'compiledSeparator', 'compiledExtension' => '.compile', 'compileAlways' => true, 'stat' => 'stat']));
$config->set('projectPath', '"projectPath"');
$config->set('environment', '"dev"');
$config->set('ng.app.name', '"ngtest"');
$dependencyInjection->set('config', $config);
$view->shouldReceive('setViewsDir')->with($modulePath . '/views/')->once();
$view->shouldReceive('registerEngines')->once();
$view->shouldReceive('setRenderLevel')->with(View::LEVEL_ACTION_VIEW)->once();
$view->shouldReceive('setVar')->with('ngAppName', 'ngtest')->once();
$view->shouldReceive('setDI');
$registerView = new RegisterView($view, $dependencyInjection);
$registerView->register($moduleName, $modulePath);
$view->mockery_verify();
$this->assertEquals($view, $dependencyInjection->get('view'));
}
示例9: 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);
}
示例10: prepare
/**
* Prepare controller.
*
* @param string|null $action Action name.
*
* @return void
*/
public function prepare($action = self::DEFAULT_ACTION)
{
$this->di = DI::getDefault();
$this->dispatcher = $this->di->get('dispatcher');
$this->cacheData = $this->di->get('cacheData');
if ($this->_widgetName !== null) {
/** @var \Engine\View $view */
$this->view = $view = $this->di->get('view');
$view->reset()->restoreViewDir()->setVars([], false)->disableLevel(View::LEVEL_LAYOUT)->disableLevel(View::LEVEL_MAIN_LAYOUT);
$viewPath = null;
if ($this->_widgetModule !== null) {
// Widget from module.
$viewPath = $this->_widgetModule . '/Widget/' . $this->_widgetName . '/' . $action;
} else {
// External widget.
$viewPath = '../widgets/' . $this->_widgetName . '/' . $action;
}
$view->pick($viewPath);
}
// run init function
if (method_exists($this, 'initialize')) {
$this->initialize();
}
}
示例11: _initEngine
/**
* Init engine.
*
* @param DI $di Dependency Injection.
*
* @return void
*/
protected function _initEngine($di)
{
foreach ($di->get('registry')->modules as $module) {
// Initialize module api.
$di->setShared(strtolower($module), function () use($module, $di) {
return new ApiInjector($module, $di);
});
}
$di->setShared('transactions', function () {
return new TxManager();
});
$di->setShared('assets', new AssetsManager($di));
$di->setShared('widgets', new Catalog());
}
示例12: setUp
/**
*
*/
public function setUp()
{
$this->di = DI::getDefault();
$bootstrap = new Bootstrap($this->di->get('config'));
$this->bootstrap = $bootstrap;
}
示例13: __construct
/**
* @param DI $container
*/
public function __construct(DI $container)
{
$this->container = $container;
$this->config = $container->get('config');
}
示例14: _initRouter
/**
* Init router.
*
* @param DI $di Dependency Injection.
* @param Config $config Config object.
*
* @return Router
*/
protected function _initRouter($di, $config)
{
$cacheData = $di->get('cacheData');
$router = $cacheData->get(EnSystem::CACHE_KEY_ROUTER_DATA);
if ($router == null) {
$saveToCache = $router === null;
// Load all controllers of all modules for routing system.
$modules = $di->get('registry')->modules;
// Use the annotations router.
$router = new PhRouterAnnotations(false);
$router->setDefaultModule(Application::SYSTEM_DEFAULT_MODULE);
$router->setDefaultNamespace(ucfirst(Application::SYSTEM_DEFAULT_MODULE) . '\\Controller');
$router->setDefaultController('Index');
$router->setDefaultAction('index');
/**
* Load all route from router file
*/
foreach ($modules as $module) {
$moduleName = ucfirst($module);
// Get all file names.
$moduleDir = opendir($di->get('registry')->directories->modules . $moduleName . '/Controller');
while ($file = readdir($moduleDir)) {
if ($file == "." || $file == ".." || strpos($file, 'Controller.php') === false) {
continue;
}
$controller = $moduleName . '\\Controller\\' . str_replace('Controller.php', '', $file);
$router->addModuleResource(strtolower($module), $controller);
}
closedir($moduleDir);
}
if ($saveToCache) {
$cacheData->save(EnSystem::CACHE_KEY_ROUTER_DATA, $router, 2592000);
// 30 days cache
}
}
$router->removeExtraSlashes(true);
$di->set('router', $router);
return $router;
}
示例15: _initLoader
/**
* Init loader.
*
* @param DI $di Dependency Injection.
* @param Config $config Config object.
* @param EventsManager $eventsManager Event manager.
*
* @return Loader
*/
protected function _initLoader($di, $config, $eventsManager)
{
// Add all required namespaces and modules.
$registry = $di->get('registry');
$namespaces = [];
$namespaces['Controllers'] = $registry->directories->modules . '/controller/';
$namespaces['Models'] = $registry->directories->modules . '/models/';
$namespaces['Cli'] = $registry->directories->library . '/cli/';
$namespaces['Engine'] = $registry->directories->engine;
$dirs['libraryDir'] = $registry->directories->library;
$dirs['engineDir'] = $registry->directories->engine;
$dirs['modulesDir'] = $registry->directories->modules;
$loader = new Loader();
$loader->registerDirs($dirs);
$loader->registerNamespaces($namespaces);
$loader->register();
$di->set('loader', $loader);
return $loader;
}