本文整理汇总了PHP中Phalcon\DiInterface::setShared方法的典型用法代码示例。如果您正苦于以下问题:PHP DiInterface::setShared方法的具体用法?PHP DiInterface::setShared怎么用?PHP DiInterface::setShared使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\DiInterface
的用法示例。
在下文中一共展示了DiInterface::setShared方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: registerServices
/**
* Registers services related to the module
*
* @param DiInterface $di Dependency Injection Container
*/
public function registerServices(DiInterface $di)
{
/**
* Read configuration
*/
$config = (require __DIR__ . "/config/config.php");
/**
* Setting up the view component
*/
$di->setShared('view', function () {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->setTemplateBefore('main');
$view->registerEngines([".volt" => function ($view, $di) {
$volt = new Volt($view, $di);
$volt->setOptions(['compiledPath' => function ($templatePath) {
return realpath(__DIR__ . "/../../var/volt") . '/' . md5($templatePath) . '.php';
}, 'compiledExtension' => '.php', 'compiledSeparator' => '%']);
return $volt;
}]);
return $view;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->setShared('db', function () use($config) {
return new Connection(['host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname]);
});
}
示例2: registerServices
public function registerServices(DiInterface $di)
{
global $config;
$di->setShared('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri($config->backend->baseUri);
return $url;
});
$di->setShared('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Multiple\\Backend\\Controllers");
return $dispatcher;
});
$di->setShared('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->backend->viewsDir);
$view->setLayoutsDir('layouts/');
$view->setPartialsDir('partials/');
$view->registerEngines(array('.phtml' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->backend->cacheDir, 'compiledSeparator' => '_'));
return $volt;
}, '.volt' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
});
}
示例3: registerServices
public function registerServices(\Phalcon\DiInterface $di)
{
//registrando annotations
$this->registerAnnotationsFiles(['Column', 'Entity', 'GeneratedValue', 'HasLifecycleCallbacks', 'Id', 'PrePersist', 'PreUpdate', 'Table', 'ManyToOne', 'ManyToMany', 'JoinTable', 'JoinColumn']);
//configurando entity manager
$di->setShared('entityManager', function () use($di) {
$infraConfig = $di->get('Infrastructure\\Config');
// $doctrine_config = Setup::createAnnotationMetadataConfiguration($infraConfig['ormMapper'], $di->get('App\Config')['devmode']);
$config = Setup::createConfiguration($di->get('App\\Config')['devmode']);
$driver = new AnnotationDriver(new AnnotationReader(), $infraConfig['ormMapper']);
// registering noop annotation autoloader - allow all annotations by default
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);
$entityManager = EntityManager::create($infraConfig['databases'][0], $config);
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
return $entityManager;
});
$di->setShared('api', function () use($di) {
$infraConfig = $di->get('Infrastructure\\Config');
return new Service\RESTClient($infraConfig['baseUrl']['api'], $infraConfig['credentials']);
});
$di->setShared('geocodeApi', function () use($di) {
return new Service\RESTClient('https://maps.googleapis.com/maps/api/geocode/json?&key=AIzaSyBwFWzpssaahZ7SfLZt6mv7PeZBFXImpmo&address=');
});
}
示例4: registerServices
/**
* Registers services related to the module
*
* @param DiInterface $di
*/
public function registerServices(DiInterface $di)
{
/**
* 获取全局配置
*/
$config = $di->has('config') ? $di->getShared('config') : null;
/**
* 各模块可自定义config文件并替换全局的config配置
*/
if (file_exists($this->modulePath . '/config/config.php')) {
$override = new Config(include $this->modulePath . '/config/config.php');
if ($config instanceof Config) {
$config->merge($override);
} else {
$config = $override;
}
}
//重置全局配置
$di->setShared('config', $config);
/**
* 设置各个模块的视图目录
*/
$view = new View();
//$view->setViewsDir($this->modulePath . '/views/default/');
$view->setViewsDir(FRONTEND_PUBLIC_PATH . 'views/default/');
$view->registerEngines(['.volt' => 'volt', '.php' => 'volt', '.html' => 'volt']);
$di->set('view', $view);
}
示例5: registerServices
/**
* {@inheritdoc}
*
* @param \Phalcon\DI $di
*/
public function registerServices(DiInterface $di = null)
{
$di->getDispatcher()->setDefaultNamespace('Webird\\Web\\Controllers');
$di->setShared('view', $this->getViewFunc($di));
if (DEV_ENV === ENV) {
$debugPanel = new DebugPanel($di);
}
// //Listen for events produced in the dispatcher using the Security plugin
// $evManager = $di->getShared('eventsManager');
//
// $evManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) use ($di) {
//
// switch ($exception->getCode()) {
//
// case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
// case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
//
// $dispatcher->forward([
// 'controller' => 'errors',
// 'action' => 'show404',
// ]);
//
// return FALSE;
// break;
// }
// });
}
示例6: setDI
public function setDI(DiInterface $di)
{
$di->setShared('config', static::$config);
$this->initEventsManager($di);
parent::setDI($di);
Di::setDefault($di);
}
示例7: initConfig
/**
* Initialize the Application Config.
*/
protected function initConfig()
{
$this->di->setShared('config', function () {
$path = BASE_DIR . 'app/config/';
if (!is_readable($path . 'config.php')) {
throw new RuntimeException('Unable to read config from ' . $path . 'config.php');
}
$config = (include $path . 'config.php');
if (is_array($config)) {
$config = new Config($config);
}
if (!$config instanceof Config) {
$type = gettype($config);
if ($type == 'boolean') {
$type .= $type ? ' (true)' : ' (false)';
} elseif (is_object($type)) {
$type = get_class($type);
}
throw new RuntimeException(sprintf('Unable to read config file. Config must be either an array or Phalcon\\Config instance. Got %s', $type));
}
if (is_readable($path . APPLICATION_ENV . '.php')) {
$override = (include_once $path . APPLICATION_ENV . '.php');
if (is_array($override)) {
$override = new Config($override);
}
if ($override instanceof Config) {
$config->merge($override);
}
}
return $config;
});
}
示例8: registerServices
/**
* @param \Phalcon\DiInterface $di
*
* @return \Phalcon\DiInterface
*/
public function registerServices($di)
{
$this->registerAutoloaders();
$di->set('view', function () use($di) {
$view = new View();
$view->setViewsDir(AUTOADMINMODULEROOT . '/views/');
return $view;
});
$di->setShared('entityManager', function () use($di) {
return new EntityManager();
});
$di->setShared('assets', function () {
return new AssetsManager(['sourceBasePath' => AUTOADMINMODULEROOT . '/assets/', 'targetBasePath' => PUBLICROOT . '/assets/']);
});
return $di;
}
示例9: registerServices
/**
* Register the services here to make them general
* or register in the ModuleDefinition to make them module-specific
*/
public function registerServices(DiInterface $di)
{
//Read configuration
$config = (include __DIR__ . "/config/config.php");
// The URL component is used to generate all kind of urls in the application
$di->set('url', function () use($config) {
$url = new Url();
$url->setBaseUri($config->application->baseUri);
return $url;
});
//Registering a dispatcher
$di->set('dispatcher', function () {
//Create/Get an EventManager
$eventsManager = new EventsManager();
//Attach a listener
$eventsManager->attach('dispatch', function ($event, $dispatcher, $exception) {
//controller or action doesn't exist
if ($event->getType() == 'beforeException') {
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(['module' => 'backend', 'controller' => 'errors', 'action' => 'notFound']);
return false;
}
}
});
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Phanbook\\Backend\\Controllers");
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
/**
* Setting up the view component
*/
$di->set('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
$view->registerEngines(['.volt' => 'volt']);
// Create an event manager
$eventsManager = new EventsManager();
// Attach a listener for type 'view'
$eventsManager->attach('view', function ($event, $view) {
if ($event->getType() == 'notFoundView') {
throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');
}
});
// Bind the eventsManager to the view component
$view->setEventsManager($eventsManager);
return $view;
});
$configMenu = (include __DIR__ . "/config/config.menu.php");
$di->setShared('menuStruct', function () use($configMenu) {
// if structure received from db table instead getting from $config
// we need to store it to cache for reducing db connections
$struct = $configMenu->get('menuStruct')->toArray();
return $struct;
});
}
示例10: registerServices
/**
* @param \Phalcon\DiInterface $di
*/
public function registerServices(\Phalcon\DiInterface $di = null)
{
// Set up MVC dispatcher.
$controller_class = 'Modules\\' . $this->_module_class_name . '\\Controllers';
$di['dispatcher'] = function () use($controller_class) {
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) {
// Set odd/even pairs as the key and value of parameters, respectively.
$keyParams = array();
$params = $dispatcher->getParams();
foreach ($params as $position => $value) {
if (is_int($position)) {
if ($position & 1) {
$keyParams[$params[$position - 1]] = urldecode($value);
}
} else {
// The "name" parameter is internal to routes.
if ($position != 'name') {
$keyParams[$position] = urldecode($value);
}
}
}
$dispatcher->setParams($keyParams);
// Detect filename in controller and convert to "format" parameter.
$controller_name = $dispatcher->getControllerName();
if (strstr($controller_name, '.') !== false) {
list($controller_clean, $format) = explode('.', $controller_name, 2);
$dispatcher->setControllerName($controller_clean);
$dispatcher->setParam('format', $format);
}
// Detect filename in action and convert to "format" parameter.
$action_name = $dispatcher->getActionName();
if (strstr($action_name, '.') !== false) {
list($action_clean, $format) = explode('.', $action_name, 2);
$dispatcher->setActionName($action_clean);
$dispatcher->setParam('format', $format);
}
});
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
$dispatcher->setDefaultNamespace($controller_class);
return $dispatcher;
};
// Set up module-specific configuration.
$module_base_name = strtolower($this->_module_class_name);
$module_config = $di->get('module_config');
$di->setShared('current_module_config', function () use($module_base_name, $module_config) {
if (isset($module_config[$module_base_name])) {
return $module_config[$module_base_name];
} else {
return null;
}
});
// Set up the view component and shared templates.
$views_dir = 'modules/' . $module_base_name . '/views/scripts/';
$di['view'] = function () use($views_dir) {
return \FA\Phalcon\View::getView(array('views_dir' => $views_dir));
};
}
示例11: registerServices
/**
* Registers the module-only services
*
* @param \Phalcon\DiInterface $di
*/
public function registerServices($di)
{
/**
* Read application wide and module only configurations
*/
$appConfig = $di->get('config');
$moduleConfig = (include __DIR__ . '/config/config.php');
$di->set('moduleConfig', $moduleConfig);
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use($appConfig) {
$url = new UrlResolver();
$url->setBaseUri($appConfig->application->baseUri);
return $url;
});
/**
* Module specific dispatcher
*/
$di->set('dispatcher', function () use($di) {
$dispatcher = new Dispatcher();
$dispatcher->setEventsManager($di->getShared('eventsManager'));
$dispatcher->setDefaultNamespace('App\\Modules\\Frontend\\');
return $dispatcher;
});
$di->setShared('request', function () use($appConfig) {
return new \Phalcon\Http\Request();
});
/**
* Include config per environment
*/
include __DIR__ . '/config/config_' . $appConfig->application->environment . '.php';
$database = $di->getConfig()->application->site . $di->get('request')->getQuery("country_code");
/**
* Simple database connection to localhost
*/
$di->setShared('mongo', function ($config, $database) {
$mongo = new \Mongo();
return $mongo->selectDb($config->{$database}->dbname);
}, true);
$di->setShared('collectionManager', function () {
return new \Phalcon\Mvc\Collection\Manager();
}, true);
}
示例12: init
public static function init(Di $di, $defaultTube = 'default')
{
$config = $di->get('config');
if (!isset($config->queue)) {
return;
}
$params = (array) $config->queue;
$di->setShared(self::SERVICE_NAME, function () use($params, $defaultTube) {
$beanstalk = new Beanstalk($params);
$beanstalk->choose($defaultTube);
return $beanstalk;
});
}
示例13: registerServices
/**
* Registers services related to the module
*
* @param DiInterface $di
*/
public function registerServices(DiInterface $di)
{
/**
* Read common configuration
*/
$config = $di->has('config') ? $di->getShared('config') : null;
/**
* Try to load local configuration
*/
if (file_exists(__DIR__ . '/config/config.php')) {
$override = new Config(include __DIR__ . '/config/config.php');
if ($config instanceof Config) {
$config->merge($override);
} else {
$config = $override;
}
}
$di->setShared('config', $config);
/**
* Setting up the view component
*/
$view = $di->get('view');
$view->setViewsDir($config->get('application')->viewsDir);
$di->set('view', $view);
// register helper
$di->setShared('adminHelper', function () {
return new \Application\Admin\Librarys\voltHelper();
});
// add default namespace
$dispatcher = $di->get('dispatcher');
$dispatcher->setDefaultNamespace("Application\\Admin\\Controllers");
$di->set('dispatcher', $dispatcher);
// register menu
$di->set('AdminMenus', function () {
return require __DIR__ . '/config/menus.php';
});
}
示例14: registerServices
/**
* Register the services here to make them general or register in the ModuleDefinition to make them module-specific
*/
public function registerServices(DiInterface $di)
{
// Setup the Dispatcher service
$di->get('dispatcher')->setDefaultNamespace("App\\Backend\\Controllers\\");
// Registering a Smarty shared-service
$di->setShared('smarty', function () use($di) {
$smarty = new \Smarty();
$options = ['left_delimiter' => '<{', 'right_delimiter' => '}>', 'template_dir' => ROOT_DIR . '/app/Backend/Views', 'compile_dir' => ROOT_DIR . '/runtime/Smarty/compile', 'cache_dir' => ROOT_DIR . '/runtime/Smarty/cache', 'error_reporting' => error_reporting() ^ E_NOTICE, 'escape_html' => true, 'force_compile' => false, 'compile_check' => true, 'caching' => false, 'debugging' => true];
foreach ($options as $k => $v) {
$smarty->{$k} = $v;
}
$BaseUri = './';
$smarty->assign('BaseUri', $BaseUri);
return $smarty;
});
// Registering a Html shared-service
$di->setShared('html', function () use($di) {
$smarty = $di->get('smarty');
$router = $di->get('router');
$controller = $router->getControllerName();
$action = $router->getActionName();
return $smarty->display(implode('/', [$controller, $action]) . '.html');
});
}
示例15: registerServices
/**
* Registers services related to the module
*
* @param DiInterface $di
*/
public function registerServices(DiInterface $di)
{
/**
* Read common configuration
*/
$config = $di->has('config') ? $di->getShared('config') : null;
/**
* Try to load local configuration
*/
if (file_exists(__DIR__ . '/config/config.php')) {
$override = new Config(include __DIR__ . '/config/config.php');
if ($config instanceof Config) {
$config->merge($override);
} else {
$config = $override;
}
}
/**
* Setting up the view component
*/
$view = $di->get('view');
$view->setViewsDir($config->get('application')->viewsDir);
$di->set('view', $view);
// register helper
$di->setShared('adminHelper', function () {
return new \Application\Admin\Librarys\voltHelper();
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
$config = $config->database->toArray();
$dbAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config['adapter'];
unset($config['adapter']);
return new $dbAdapter($config);
};
// add default namespace
$dispatcher = $di->get('dispatcher');
$dispatcher->setDefaultNamespace("Application\\Admin\\Controllers");
$di->set('dispatcher', $dispatcher);
// register menu
$di->set('AdminMenus', function () {
return require __DIR__ . '/config/menus.php';
});
}