本文整理汇总了PHP中Phalcon\Di类的典型用法代码示例。如果您正苦于以下问题:PHP Di类的具体用法?PHP Di怎么用?PHP Di使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Di类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addComment
public function addComment($data)
{
/** @var myModel $this */
$comment = new Comments();
$comment->content = $data['content'];
$comment->commentable_id = $this->id;
$comment->commentable_type = get_class($this);
// $comment->user_id = $this->getDI()->getShared('session')->get('auth')['id'];//获得当前登录对象的id
$user = \Phalcon\Di::getDefault()->get('auth');
$comment->user_id = $user->id;
//获得当前登录对象的id
// dd($comment);
$comment->save();
/** @var myModel $this */
if (method_exists($this, 'increaseCount')) {
$this->increaseCount('commentCount');
} else {
$this->save();
//更新时间
}
if (is_a($this, 'Tags')) {
$meta = $this->getTagmetaOrNew();
$meta->save();
}
return $this;
}
示例2: register
public static function register(Di $di)
{
static::$di = $di;
ini_set('session.use_cookies', 0);
ini_set('session.cache_limiter', '');
$di->remove('session');
static::$session = null;
$di->setShared('session', function () {
$default = Config::get('session.default');
$config = Config::get('session.drivers.' . $default);
$class = $config['adapter'];
$options = $config['options'];
$options += Config::get('session.options');
$options['cookies'] += Config::get('cookies');
session_name($options['cookies']['name']);
strpos($class, '\\') === false and $class = 'Phwoolcon\\Session\\Adapter\\' . $class;
$session = new $class($options);
// @codeCoverageIgnoreStart
if (!$session instanceof AdapterInterface) {
throw new SessionException('Session class should implement ' . AdapterInterface::class);
}
// @codeCoverageIgnoreEnd
return $session;
});
}
示例3: register
/**
* Fix over clever di service resolver in phalcon 2.1.x:
* let definition = \Closure::bind(definition, dependencyInjector)
* which leads to php warning "Cannot bind an instance to a static closure"
*
* @param Di $di
* @codeCoverageIgnore
*/
public static function register(Di $di)
{
if ($_SERVER['PHWOOLCON_PHALCON_VERSION'] > '2010000') {
$di->setInternalEventsManager($di->getShared('eventsManager'));
Events::attach('di:beforeServiceResolve', function (Event $event) {
/* @var Di $di */
$di = $event->getSource();
$data = $event->getData();
$name = $data['name'];
$parameters = $data['parameters'];
if (!isset($di->_services[$name])) {
return false;
}
/* @var Di\Service $service */
$service = $di->_services[$name];
if (!$service->isShared()) {
return false;
}
if (!($definition = $service->getDefinition()) instanceof Closure) {
return false;
}
return $parameters ? call_user_func_array($definition, $parameters) : call_user_func($definition);
});
}
}
示例4: register
public static function register(Di $di)
{
static::$di = $di;
static::$config = Config::get('auth');
$di->setShared('auth', function () {
$di = static::$di;
$config = static::$config;
$class = $config['adapter'];
$options = $config['options'];
strpos($class, '\\') === false and $class = 'Phwoolcon\\Auth\\Adapter\\' . $class;
if ($di->has($class)) {
$class = $di->getRaw($class);
}
if (!class_exists($class)) {
throw new Exception('Admin auth adapter class should implement ' . AdapterInterface::class);
}
/* @var Security $hasher */
$hasher = static::$di->getShared('security');
$hasher->setDefaultHash($options['security']['default_hash']);
$hasher->setWorkFactor($options['security']['work_factor']);
$adapter = new $class($options, $hasher, $di);
if (!$adapter instanceof AdapterInterface) {
throw new Exception('Auth adapter class should implement ' . AdapterInterface::class);
}
return $adapter;
});
static::addPhwoolconJsOptions();
}
示例5: injectTo
public static function injectTo(Di $di)
{
/** @var Dispatcher $dispatcher */
$dispatcher = $di->getShared('dispatcher');
$dispatcher->getEventsManager()->attach('dispatch:beforeException', function (Event $event, Dispatcher $dispatcher, \Exception $e) {
/** @var \Phalcon\Logger\AdapterInterface $logger */
$logger = $dispatcher->getDI()->get('logger');
$logger->error($e->getMessage());
if ($dispatcher instanceof Mvc\Dispatcher) {
if ($e instanceof Mvc\Dispatcher\Exception) {
$action = 'notFound';
} else {
$action = 'fatal';
if ($dispatcher->getDI()->has('response')) {
/** @var \Phalcon\Http\Response $response */
$response = $dispatcher->getDI()->get('response');
$response->setStatusCode(500, "Internal Server Error");
}
}
$dispatcher->setNamespaceName($dispatcher->getDefaultNamespace());
$dispatcher->forward(['controller' => 'error', 'action' => $action]);
}
return false;
});
}
示例6: register
public static function register(Di $di)
{
$environment = isset($_SERVER['PHWOOLCON_ENV']) ? $_SERVER['PHWOOLCON_ENV'] : 'production';
// @codeCoverageIgnoreStart
if (is_file($cacheFile = storagePath('cache/config-' . $environment . '.php'))) {
static::$config = (include $cacheFile);
Config::get('app.cache_config') or static::clearCache();
return;
}
// @codeCoverageIgnoreEnd
$defaultFiles = glob($_SERVER['PHWOOLCON_CONFIG_PATH'] . '/*.php');
$environmentFiles = glob($_SERVER['PHWOOLCON_CONFIG_PATH'] . '/' . $environment . '/*.php');
$config = new PhalconConfig(static::loadFiles($defaultFiles));
$environmentSettings = static::loadFiles($environmentFiles);
$environmentSettings['environment'] = $environment;
$environmentConfig = new PhalconConfig($environmentSettings);
$config->merge($environmentConfig);
$di->remove('config');
$di->setShared('config', $config);
static::$config = $config->toArray();
Config::get('database.default') and static::loadDb($config);
// @codeCoverageIgnoreStart
if (Config::get('app.cache_config')) {
is_dir($cacheDir = dirname($cacheFile)) or mkdir($cacheDir, 0777, true);
fileSaveArray($cacheFile, static::$config, function ($content) {
$replacement = <<<'EOF'
$_SERVER['PHWOOLCON_ROOT_PATH'] . '
EOF;
return str_replace("'{$_SERVER['PHWOOLCON_ROOT_PATH']}", $replacement, $content);
});
}
// @codeCoverageIgnoreEnd
}
示例7: 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();
}
示例8: 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;
}
示例9: getFlash
/**
* Return flash instance
*/
protected function getFlash()
{
$flash = new Session($this->classes);
$di = new Di();
$di->setShared('session', new PhalconMemorySession());
$di->setShared('escaper', new Escaper());
$flash->setDI($di);
return $flash;
}
示例10: 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);
}
示例11: 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;
}
示例12: injectTo
public static function injectTo(Di $di)
{
/** @var Dispatcher $dispatcher */
$dispatcher = $di->getShared('dispatcher');
$dispatcher->getEventsManager()->attach('dispatch:beforeException', function (Event $event, Dispatcher $dispatcher, \Throwable $e) {
if ($dispatcher instanceof Mvc\Dispatcher) {
$dispatcher->setNamespaceName($dispatcher->getDefaultNamespace());
$dispatcher->forward(['controller' => 'error', 'action' => 'index', 'params' => ['exception' => $e]]);
return false;
}
});
}
示例13: injectTo
public static function injectTo(Di $di)
{
$di->setShared('logger', function () use($di) {
$logger = new Multiple();
$config = $di->get('config')['loggers'];
foreach ($config as $logConfig) {
$adapter = $logConfig['adapter'];
$options = isset($logConfig['options']) ? $logConfig['options'] : null;
$logger->push(new $adapter($logConfig['name'], $options));
}
return $logger;
});
}
示例14: register
public static function register(Di $di)
{
static::$di = $di;
$di->remove('counter');
static::$adapter = null;
$di->setShared('counter', function () {
$default = Config::get('counter.default');
$config = Config::get('counter.drivers.' . $default);
$class = $config['adapter'];
$options = $config['options'];
strpos($class, '\\') === false and $class = 'Phwoolcon\\Util\\Counter\\' . $class;
return new $class($options);
});
}
示例15: run
public function run()
{
$log = new Stream('php://stdout');
/** @var Config $config */
$config = Di::getDefault()->getShared('config');
$expireDate = new DateTime('now', new DateTimeZone('UTC'));
$expireDate->modify('+1 month');
$baseUrl = rtrim($config->get('site')->url, '/');
$content = <<<EOL
User-agent: *
Allow: /
Sitemap: {$baseUrl}/sitemap.xml
EOL;
$adapter = new Local(dirname(dirname(__FILE__)) . '/public');
$filesystem = new Filesystem($adapter);
if ($filesystem->has('robots.txt')) {
$result = $filesystem->update('robots.txt', $content);
} else {
$result = $filesystem->write('robots.txt', $content);
}
if ($result) {
$log->info('The robots.txt was successfully updated');
} else {
$log->error('Failed to update the robots.txt file');
}
}