本文整理汇总了PHP中Phalcon\Di::set方法的典型用法代码示例。如果您正苦于以下问题:PHP Di::set方法的具体用法?PHP Di::set怎么用?PHP Di::set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\Di
的用法示例。
在下文中一共展示了Di::set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: attachVoltService
public function attachVoltService($voltCacheDir)
{
$this->_di->set('voltService', function ($view, $di) use($voltCacheDir) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(['compiledPath' => $voltCacheDir, 'compiledSeparator' => '_', 'compiledExtension' => '.compiled']);
return $volt;
});
return $this;
}
示例2: _before
/**
* executed before each test
*/
protected function _before()
{
$this->previousDependencyInjector = Di::getDefault();
$di = new Di();
$di->set('modelsManager', new ModelManager());
$di->set('collectionManager', new CollectionManager());
if ($this->previousDependencyInjector instanceof DiInterface) {
Di::setDefault($di);
}
}
示例3: testValidateIncorrectFieldType
/**
* @expectedException \Phalcon\Mvc\Model\Exception
* @expectedExceptionMessage Field name must be a string
*/
public function testValidateIncorrectFieldType()
{
$di = new Di();
$di->set('modelsManager', new Manager());
require_once __DIR__ . '/resources/TestCardNumberIncorrectField.php';
$obj = new \TestCardNumberIncorrectField();
$obj->validation();
}
示例4: getDi
/**
* Setup viewCache service and DI
* @return Di
*/
protected function getDi()
{
$di = new Di();
$di->set('viewCache', function () {
return new File(new Output(['lifetime' => 2]), ['cacheDir' => PATH_CACHE]);
});
return $di;
}
示例5: testResolvingService
/**
* Tests resolving service
*
* @author Serghei Iakovlev <serghei@phalconphp.com>
* @since 2016-01-29
*/
public function testResolvingService()
{
$this->specify("Di does not resolves service correctly", function () {
$di = new Di();
$di->set('resolved', function () {
return new \SomeService();
});
$di->set('notResolved', function () {
return new \SomeService();
});
expect($di->getService('resolved')->isResolved())->false();
expect($di->getService('notResolved')->isResolved())->false();
$di->get('resolved');
expect($di->getService('resolved')->isResolved())->true();
expect($di->getService('notResolved')->isResolved())->false();
});
}
示例6: setUp
/**
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->checkExtension('phalcon');
// 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 Escaper();
});
$this->di = $di;
}
示例7: testAvailableUniquenessWithDefaultDI
public function testAvailableUniquenessWithDefaultDI()
{
$di = new Di();
$di->set('db', $this->getDbStub());
$uniquenessOptions = ['table' => 'users', 'column' => 'login'];
$uniqueness = new Uniqueness($uniquenessOptions);
$this->validation->add('login', $uniqueness);
$messages = $this->validation->validate(['login' => 'login_free']);
$this->assertCount(0, $messages);
}
示例8: getRequestObject
/**
* Initializes the request object and returns it
*
* @author Nikolaos Dimopoulos <nikos@phalconphp.com>
* @since 2014-10-05
*
* @return Request
*/
protected function getRequestObject()
{
Di::reset();
$di = new Di();
$di->set('filter', function () {
return new Filter();
});
$request = new Request();
$request->setDI($di);
return $request;
}
示例9: register
public static function register(Di $di)
{
static::$di = $di;
$di->set('Phalcon\\Http\\Cookie', 'Phwoolcon\\Http\\Cookie');
static::$cookies = static::$di->getShared('cookies');
static::$cookies->reset();
static::$options = $options = Config::get('cookies');
static::$cookies->useEncryption($encrypt = $options['encrypt']);
$encrypt and static::$di->getShared('crypt')->setKey($options['encrypt_key'])->setPadding(Crypt::PADDING_ZERO);
/* @var \Phalcon\Http\Response $response */
if ($response = $di->getShared('response')) {
$response->setCookies(static::$cookies);
}
Events::attach('view:generatePhwoolconJsOptions', function (Event $event) {
$options = $event->getData() ?: [];
$options['cookies'] = ['domain' => static::$options['domain'], 'path' => static::$options['path']];
$event->setData($options);
return $options;
});
}
示例10: _getDI
private function _getDI()
{
$di = new Di();
$frontendCache = new FrontendCache(array('lifetime' => 2));
$backendCache = new BackendCache($frontendCache, array('cacheDir' => 'unit-tests/cache/'));
$di->set('viewCache', $backendCache);
return $di;
}
示例11: testValidateCustomMessage
public function testValidateCustomMessage()
{
$di = new Di();
$di->set('modelsManager', new Manager());
require_once __DIR__ . '/resources/TestBetweenModel.php';
$obj = new \TestBetweenModel();
$obj->min = 1;
$obj->max = 2;
$obj->position = 3;
$obj->message = 'test 123';
$obj->validation();
$messages = $obj->getMessages();
$this->assertEquals($messages[0]->getMessage(), 'test 123');
}
示例12: setupDI
/**
* Sets the environment
*/
private function setupDI()
{
Di::reset();
$di = new Di();
$di->set('router', function () {
$router = new Router(false);
$router->add('/admin/:controller/p/:action', ['controller' => 1, 'action' => 2])->setName('adminProducts');
$router->add('/api/classes/{class}')->setName('classApi');
$router->add('/{year}/{month}/{title}')->setName('blogPost');
$router->add('/wiki/{article:[a-z]+}')->setName('wikipedia');
$router->add('/news/{country:[a-z]{2}}/([a-z+])/([a-z\\-+])/{page}', ['section' => 2, 'article' => 3])->setName('news');
$router->add('/([a-z]{2})/([a-zA-Z0-9_-]+)(/|)', ['lang' => 1, 'module' => 'main', 'controller' => 2, 'action' => 'index'])->setName('lang-controller');
$router->removeExtraSlashes(true);
return $router;
});
return $di;
}
示例13: testDispatcher
public function testDispatcher()
{
$this->specify("Dispatcher doesn't throw exception when handler can't be loaded", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("index");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "IndexController handler class cannot be loaded"]]);
$this->specify("Dispatcher doesn't throw exception when handler can't be loaded (2)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("essai");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "EssaiController handler class cannot be loaded"]]);
$this->specify("Dispatcher doesn't throw exception when handler can't be loaded (3)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test0");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Test0Controller handler class cannot be loaded"]]);
$this->specify("Dispatcher doesn't throw exception when action can't be found", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test1");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Action 'index' was not found on handler 'test1'"]]);
$this->specify("Dispatcher doesn't return controller after dispatching", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$controller = $dispatcher->dispatch();
expect($controller)->isInstanceOf("Test2Controller");
});
$this->specify("Dispatcher doesn't throw exception when action can't be found (2)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("essai");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Action 'essai' was not found on handler 'test2'"]]);
$this->specify("Dispatcher doesn't work as expected", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("other");
$dispatcher->setParams([]);
$controller = $dispatcher->dispatch();
expect($controller)->isInstanceOf("Test2Controller");
});
$this->specify("Dispatcher doesn't work as expected (2)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("another");
$dispatcher->setParams([]);
$dispatcher->dispatch();
$value = $dispatcher->getReturnedValue();
//.........这里部分代码省略.........
示例14: testModules
public function testModules()
{
$this->specify("CLI Console doesn't work with modules", function () {
$di = new Di();
$di->set('data', function () {
return "data";
});
$console = new Console();
$console->setDI($di);
$expected = ['devtools' => ['className' => 'dummy', 'path' => 'dummy_file']];
$console->registerModules($expected);
expect($console->getModules())->equals($expected);
$userModules = ['front' => ['className' => 'front', 'path' => 'front_file'], 'worker' => ['className' => 'worker', 'path' => 'worker_file']];
$expected = ['devtools' => ['className' => 'dummy', 'path' => 'dummy_file'], 'front' => ['className' => 'front', 'path' => 'front_file'], 'worker' => ['className' => 'worker', 'path' => 'worker_file']];
$console->registerModules($userModules, true);
expect($console->getModules())->equals($expected);
});
}
示例15: _getDi
private function _getDi($service = 'viewCache', $lifetime = 60)
{
$di = new Di();
$frontendCache = new FrontendCache(array('lifetime' => $lifetime));
$backendCache = new BackendCache($frontendCache, array('cacheDir' => 'unit-tests/cache/'));
$di->set($service, $backendCache);
return $di;
}