本文整理汇总了PHP中Phalcon\DI::set方法的典型用法代码示例。如果您正苦于以下问题:PHP DI::set方法的具体用法?PHP DI::set怎么用?PHP DI::set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\DI
的用法示例。
在下文中一共展示了DI::set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: set
/**
* Registers a service in the services container
*
* @param string $name
* @param mixed $definition
* @param boolean $shared
* @throws RuntimeException
* @return \Phalcon\DI\ServiceInterface
*/
public static function set($name, $definition, $shared = null)
{
if (self::$di == null) {
throw new RuntimeException('IoC container is null!');
}
self::$di->set($name, $definition, $shared);
}
示例2: 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;
}
示例3: testProfilerInitialization
public function testProfilerInitialization()
{
$this->di->set(ProfilerDataCollector::DI_NAME, function () {
return (new ProfilerManager())->setEventsManager($this->di->getShared('eventsManager'));
}, true);
$manager = $this->di->get(ProfilerDataCollector::DI_NAME);
$this->assertInstanceOf('\\Vegas\\Profiler\\Manager', $manager);
}
示例4: create
/**
* @return DummyLatteTemplateAdapter
*/
public static function create()
{
$view = new PhView();
$di = new PhDi();
$di->set('tag', new PhTag());
$di->set('security', new PhSecurity());
$di->set('url', new PhUrl());
$latteFactory = new LatteFactory();
$latteFactory->setTempDir(TEMP_DIR);
$adapter = new DummyLatteTemplateAdapter($view, $di, $latteFactory);
return $adapter;
}
示例5: register
/**
* @param string $moduleName
* @param string $modulePath
*/
public function register($moduleName, $modulePath)
{
$pPath = $this->dependencyInjection->get('config')->projectPath;
$pDir = substr($pPath, 0, strlen($pPath) - 10);
$relModulePath = substr($modulePath, strlen($pDir));
$x = implode('/', array_map(function () {
return '..';
}, explode('/', str_replace('//', '/', $relModulePath))));
$this->view->setMainView('/../' . $x . '/common/views/index');
$this->view->setPartialsDir('/../' . $x . '/common/views/partial/');
$this->view->setViewsDir($modulePath . '/views/');
$this->registerEngine();
$this->dependencyInjection->set('view', $this->view);
}
示例6: 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);
});
}
}
}
示例7: _getDI
protected function _getDI()
{
DI::reset();
$di = new DI();
$di->set('modelsManager', function () {
return new Manager();
});
$di->set('modelsMetadata', function () {
return new Memory();
});
$di->set('db', function () {
require PATH_CONFIG . 'config.db.php';
return new Mysql($configMysql);
});
return $di;
}
示例8: getFlash
/**
* Return flash instance
*/
protected function getFlash()
{
$flash = new PhFlash($this->classes);
$di = new PhDI();
$di->set('session', new Helper\InMemorySession(), true);
$flash->setDI($di);
return $flash;
}
示例9: getDi
private function getDi()
{
$di = new PhalconDi();
$di->set('request', function () {
return new PhalconRequest();
});
return $di;
}
示例10: setUpBeforeClassAndDataProviders
public static function setUpBeforeClassAndDataProviders()
{
self::$previousDependencyInjector = DI::getDefault();
$di = new DI();
$di->set('modelsMetadata', function () {
return new MemoryMetadata();
}, true);
$di->set('modelsManager', function () {
return new ModelsManager();
}, true);
$di->set('db', function () {
return new MysqlAdapter(array('host' => 'localhost', 'port' => '3306', 'username' => 'root', 'password' => '', 'dbname' => 'incubator_tests', 'charset' => 'utf8mb4'));
}, true);
if (self::$previousDependencyInjector instanceof DI) {
DI::setDefault($di);
}
spl_autoload_register(array(__CLASS__, 'autoloadModels'), true, true);
}
示例11: getRequestObject
/**
* Initializes the request object and returns it
*
* @author Nikolaos Dimopoulos <nikos@phalconphp.com>
* @since 2014-10-05
*
* @return PhRequest
*/
protected function getRequestObject()
{
PhDI::reset();
$di = new PhDI();
$di->set('filter', function () {
return new PhTFilter();
});
$request = new PhTRequest();
$request->setDI($di);
return $request;
}
示例12: setUp
/**
* Initialize testing object
*
* @uses Auth
* @uses \ReflectionClass
*/
public function setUp()
{
$this->di = new FactoryDefault();
$this->di->reset();
// Setup DI
$this->di = new DI();
$this->di->setShared('session', function () {
$session = new \Phalcon\Session\Adapter\Files();
$session->start();
return $session;
});
$this->di->set('tag', function () {
$tag = new \Phalcon\Tag();
return $tag;
});
$this->di->set('escaper', function () {
$escaper = new \Phalcon\Escaper();
return $escaper;
});
DI::setDefault($this->di);
$this->reflection = new \ReflectionClass('ULogin\\Auth');
$this->auth = new Auth();
}
示例13: registerServices
/**
* @param DI $di
*/
public function registerServices($di)
{
$this->registerAutoloaders();
$di->setShared('session', function () {
$session = new \Phalcon\Session\Adapter\Files();
$session->start();
return $session;
});
$di->set('view', function () use($di) {
$view = new View();
$view->setViewsDir(MINI_ADMIN_ROOT . '/views/');
return $view;
});
}
示例14: setUp
/**
* Sets the test up by loading the DI container and other stuff
*
* @author Nikos Dimopoulos <nikos@phalconphp.com>
* @since 2012-09-30
*
* @param \Phalcon\DiInterface $di
* @param \Phalcon\Config $config
* @return DI
*/
protected function setUp(DiInterface $di = null, Config $config = null)
{
$this->checkExtension('phalcon');
if (!is_null($config)) {
$this->config = $config;
}
if (is_null($di)) {
// Reset the DI container
DI::reset();
// Instantiate a new DI container
$di = new DI();
// Set the URL
$di->set('url', function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
});
$di->set('escaper', function () {
return new \Phalcon\Escaper();
});
}
$this->di = $di;
}
示例15: registerServices
/**
* This methods registers the services to be used by the application
*/
protected function registerServices()
{
$di = new DI();
//Registering a router
$di->set('router', function () {
return new Router();
});
//Registering a dispatcher
$di->set('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace('Single\\Controllers\\');
return $dispatcher;
});
//Registering a Http\Response
$di->set('response', function () {
return new Response();
});
//Registering a Http\Request
$di->set('request', function () {
return new Request();
});
//Registering the view component
$di->set('view', function () {
$view = new View();
$view->setViewsDir('../apps/views/');
return $view;
});
$di->set('db', function () {
return new Database(array("host" => "localhost", "username" => "root", "password" => "", "dbname" => "invo"));
});
//Registering the Models-Metadata
$di->set('modelsMetadata', function () {
return new MemoryMetaData();
});
//Registering the Models Manager
$di->set('modelsManager', function () {
return new ModelsManager();
});
$this->setDI($di);
}