本文整理汇总了PHP中Phalcon\DI\FactoryDefault::setShared方法的典型用法代码示例。如果您正苦于以下问题:PHP FactoryDefault::setShared方法的具体用法?PHP FactoryDefault::setShared怎么用?PHP FactoryDefault::setShared使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\DI\FactoryDefault
的用法示例。
在下文中一共展示了FactoryDefault::setShared方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* Constructs the app.
*
* Checks singleton instance
* Adds a dependency injector if none provided
* Sets the notFound handler
*
* @param FactoryDefault $dependencyInjector
* @throws \RuntimeException
*/
public function __construct($dependencyInjector = null)
{
if (self::$app === null) {
if ($dependencyInjector === null) {
$dependencyInjector = new FactoryDefault();
}
$dependencyInjector->setShared('response', Response::class);
$dependencyInjector->setShared('router', Router::class);
if (!$dependencyInjector->has('eventsManager')) {
$dependencyInjector->setShared('eventsManager', \Phalcon\Events\Manager::class);
}
if (!$dependencyInjector->has('request')) {
$dependencyInjector->setShared('request', \Phalcon\Http\Request::class);
}
parent::__construct($dependencyInjector);
self::$app = $this;
$this->setEventsManager($dependencyInjector->getShared('eventsManager'));
$this->addHeaderHandler(new HeaderHandler\Accept());
$app = self::$app;
$this->_errorHandler = function (\Exception $ex) {
return $this->errorHandler($ex);
};
$this->_notFoundHandler = function () {
return $this->notFoundHandler();
};
} else {
throw new \RuntimeException("Can't instance App more than once");
}
}
示例2: initView
/**
* 初始化view
*/
protected function initView()
{
$config = $this->config;
$debug = $this->debug;
$this->di->setShared('view', function () use($config, $debug) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$viewEngines = ['.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'];
if ($config->offsetExists('volt')) {
$viewEngines['.volt'] = function ($view, $di) use($config, $debug) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->volt->cacheDir, 'compiledExtension' => ".compiled", 'compiledSeparator' => '_', 'compileAlways' => $debug));
$compiler = $volt->getCompiler();
foreach ($config->volt->extension as $k => $v) {
$compiler->addExtension($v);
}
foreach ($config->volt->func as $k => $v) {
$compiler->addFunction($k, $v);
}
$filterList = $config->volt->filter;
foreach ($filterList as $k => $v) {
$compiler->addFilter($k, $v);
}
return $volt;
};
}
$view->registerEngines($viewEngines);
return $view;
});
}
示例3: __construct
/**
* Constructor.
*/
public function __construct()
{
/**
* Create default DI.
*/
$di = new DI\FactoryDefault();
/**
* Get config.
*/
$this->_config = Config::factory();
if (!$this->_config->installed) {
define('CHECK_REQUIREMENTS', true);
require_once PUBLIC_PATH . '/requirements.php';
}
/**
* Setup Registry.
*/
$registry = new Registry();
$registry->modules = array_merge([self::SYSTEM_DEFAULT_MODULE, 'user'], $this->_config->modules->toArray());
$registry->widgets = $this->_config->widgets->toArray();
$registry->directories = (object) ['engine' => ROOT_PATH . '/app/engine/', 'modules' => ROOT_PATH . '/app/modules/', 'plugins' => ROOT_PATH . '/app/plugins/', 'widgets' => ROOT_PATH . '/app/widgets/', 'libraries' => ROOT_PATH . '/app/libraries/'];
$di->set('registry', $registry);
// Store config in the DI container.
$di->setShared('config', $this->_config);
parent::__construct($di);
}
示例4: __construct
/**
* Bootstrap constructor.
*/
public function __construct()
{
$di = new FactoryDefault();
$em = new EventsManager();
$em->enablePriorities(true);
$config = $this->initConfig();
// Register the configuration itself as a service
$di->setShared('config', $config);
$this->app = new Application();
$this->initLogger($di, $config, $em);
$this->initLoader($di, $config, $em);
foreach ($this->loaders as $service) {
$serviceName = ucfirst($service);
$this->{'init' . $serviceName}($di, $config, $em);
}
$di->setShared('eventsManager', $em);
$di->setShared('app', $this->app);
$this->app->setEventsManager($em);
$this->app->setDI($di);
}
示例5: __construct
/**
* Constructor of the App
*/
public function __construct()
{
$di = new DI\FactoryDefault();
$this->_config = config('config');
$registry = new Registry();
$registry->directories = (object) ['modules' => APP_PATH . '/modules/', 'engine' => APP_PATH . '/engine/', 'library' => APP_PATH . '/library/'];
$di->set('registry', $registry);
$di->setShared('config', $this->_config);
$eventsManager = new EventsManager();
$this->setEventsManager($eventsManager);
$this->_initLogger($di, $this->_config);
$this->_initLoader($di, $this->_config, $eventsManager);
foreach ($this->_loaders as $service) {
$serviceName = ucfirst($service);
$eventsManager->fire('init:before' . $serviceName, null);
$result = $this->{'_init' . $serviceName}($di, $this->_config, $eventsManager);
$eventsManager->fire('init:after' . $serviceName, $result);
}
$di->setShared('eventsManager', $eventsManager);
$this->_noAuthPages = array();
}
示例6: mvcDi
private function mvcDi()
{
$c = $this->config();
$di = new FactoryDefault();
//TODO:url
//Register a session container
$di->setShared('session', function () use($c) {
$session = new SessionAdapter();
session_set_cookie_params(0, '/', $c->hostname);
$session->start();
return $session;
});
//Register rendering mechanism
$di->setShared('view', function () {
$view = new View();
$view->registerEngines(['.phtml' => 'PhalconZ\\Lib\\PhpViewEngine']);
return $view;
});
$di->setShared('config', $this->config());
$di->set('router', $this->mvcRouter());
$this->database($di);
return $di;
}
示例7: __construct
/**
* Constructor.
*/
public function __construct()
{
/**
* Create default DI.
*/
$di = new DI\FactoryDefault();
/**
* Get config.
*/
$this->_config = Config::factory();
/**
* Adding modules to registry to load.
* Module namespace - directory will be load from here.
*/
$registry = new PhRegistry();
$registry->modules = array_merge([self::SYSTEM_DEFAULT_MODULE], $this->_config->modules->toArray());
$registry->directories = (object) ['engine' => ROOT_PATH . '/app/engine/', 'modules' => ROOT_PATH . '/app/modules/'];
$di->set('registry', $registry);
/**
* Store config in the DI container.
*/
$di->setShared('config', $this->_config);
parent::__construct($di);
}
示例8: FactoryDefault
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Filter;
use App\Lib\Facebook\Facebook;
use App\Lib\OAuth\ApiStorage;
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new FactoryDefault();
/**
* Set shared config.
*/
$di->setShared('config', function () use($config) {
return $config;
});
/**
* The URL component is used to generate all kind of urls in the application.
*/
$di->setShared('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
});
/**
* Database connection is created based in the parameters defined in the configuration file.
*/
$di->setShared('db', function () use($config) {
$dbConfig = $config->database->toArray();
$adapter = $dbConfig['adapter'];
示例9: FactoryDefault
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Direct as Flash;
$di = new FactoryDefault();
$di->set('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("TestApp\\Controllers");
return $dispatcher;
});
$di->setShared('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
});
$di->setShared('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_'));
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
});
$di->setShared('db', function () use($config) {
$dbConfig = $config->database->toArray();
$adapter = $dbConfig['adapter'];
示例10: function
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
/**
* Setting up the view component
*/
$di->setShared('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_'));
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function () use($config) {
return new DbAdapter($config->database->toArray());
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();
示例11: function
});
} else {
if ($config->database->modelsMetadata == 'Xcache') {
$di->set('modelsMetadata', function () {
$metaData = new Phalcon\Mvc\Model\Metadata\Xcache(array('prefix' => 'igo', 'lifetime' => 86400));
return $metaData;
});
}
}
}
/**
* Start the session the first time some component request the session service
*/
$di->setShared('session', function () {
$session = new SessionAdapter();
session_name('sessionIGO');
$session->start();
return $session;
});
/**
* Ajout du routing pour le navigateur construit, en utilisant les paramètres REST plutot que KVP.
*/
$di->set('router', function () {
$router = new \Phalcon\Mvc\Router();
//Define a route
$router->add("/contexte/{contexte}", array("controller" => "igo", "action" => "contexte", "contexteid" => 1));
$router->add("/configuration/{configuration}", array("controller" => "igo", "action" => "configuration", "configuration" => 1));
$router->add("/couche/{coucheId}", array("controller" => "igo", "action" => "couche", "coucheid" => 1));
$router->add("/groupe/{groupeId}", array("controller" => "igo", "action" => "groupe", "coucheid" => 1));
$router->setDefaults(array('controller' => 'index', 'action' => 'index'));
return $router;
});
示例12: define
if ((bool) $config->develop->debug === true) {
$debug = new \Phalcon\Debug();
$debug->listen();
define('DEBUG', true);
} else {
define('DEBUG', false);
}
// Setup the view component
$di->set('view', function () {
$view = new View();
$view->setViewsDir(APP_PATH . 'app/views/');
$view->registerEngines(array(".volt" => 'Phalcon\\Mvc\\View\\Engine\\Volt', ".phtml" => 'Phalcon\\Mvc\\View\\Engine\\Volt'));
return $view;
});
$di->set('elements', function () {
return new Elements();
});
// Setup a base URI
$di->set('url', function () use($config) {
$url = new UrlProvider();
$url->setBaseUri($config->application->baseUri);
return $url;
});
// Set session
$di->setShared('session', function () {
$session = new Session();
$session->start();
return $session;
});
$debug = new \Phalcon\Debug();
$debug->listen();
示例13: main
/**
* Execute Phalcon Developer Tools
*
* @param string $path The path to the Phalcon Developer Tools
* @param string $ip Optional IP address for securing Developer Tools
* @return void
* @throws \Exception if Phalcon extension is not installed
* @throws \Exception if Phalcon version is not compatible Developer Tools
* @throws \Phalcon\Exception if Application config could not be loaded
*/
public static function main($path, $ip = null)
{
if (!extension_loaded('phalcon')) {
throw new \Exception("Phalcon extension isn't installed, follow these instructions to install it: " . 'https://docs.phalconphp.com/en/latest/reference/install.html');
}
if ($ip !== null) {
self::$ip = $ip;
}
if (!defined('TEMPLATE_PATH')) {
define('TEMPLATE_PATH', $path . '/templates');
}
$basePath = dirname(getcwd());
// Dirs for search config file
$configDirs = array($basePath . '/config/', $basePath . '/app/config/', $basePath . '/apps/frontend/config/', $basePath . '/apps/backend/config/');
$config = null;
foreach ($configDirs as $configPath) {
if (file_exists($configPath . 'config.ini')) {
$config = new ConfigIni($configPath . 'config.ini');
break;
} elseif (file_exists($configPath . 'config.php')) {
$config = (include $configPath . 'config.php');
if (is_array($config)) {
$config = new Config($config);
}
break;
} elseif (file_exists($configPath . 'config.yaml')) {
$config = new YamlConfig($configPath . 'config.yaml');
break;
} elseif (file_exists($configPath . 'config.json')) {
$config = new JsonConfig($configPath . 'config.json');
break;
}
}
if (null === $config) {
throw new Exception(sprintf("Configuration file couldn't be loaded! Scanned dirs: %s", implode(', ', $configDirs)));
}
$loader = new Loader();
$loader->registerDirs(array($path . '/scripts/', $path . '/scripts/Phalcon/Web/Tools/controllers/'));
$loader->registerNamespaces(array('Phalcon' => $path . '/scripts/'));
$loader->register();
if (Version::getId() < Script::COMPATIBLE_VERSION) {
throw new \Exception(sprintf("Your Phalcon version isn't compatible with Developer Tools, download the latest at: %s", Script::DOC_DOWNLOAD_URL));
}
try {
$di = new FactoryDefault();
$di->setShared('view', function () use($path) {
$view = new View();
$view->setViewsDir($path . '/scripts/Phalcon/Web/Tools/views/');
return $view;
});
$di->setShared('config', $config);
$di->setShared('url', function () use($config) {
$url = new Url();
if (isset($config->application->baseUri)) {
$baseUri = $config->application->baseUri;
} elseif (isset($config->baseUri)) {
$baseUri = $config->baseUri;
} else {
$baseUri = '/';
}
$url->setBaseUri($baseUri);
return $url;
});
$di->setShared('flash', function () {
return new Flash(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning'));
});
$di->setShared('db', function () use($config) {
if (isset($config->database->adapter)) {
$adapter = $config->database->adapter;
} else {
$adapter = 'Mysql';
}
if (is_object($config->database)) {
$configArray = $config->database->toArray();
} else {
$configArray = $config->database;
}
$className = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
unset($configArray['adapter']);
return new $className($configArray);
});
self::$di = $di;
$app = new Application();
$app->setDi($di);
echo $app->handle()->getContent();
} catch (Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "<br>";
echo nl2br($e->getTraceAsString());
} catch (\PDOException $e) {
echo get_class($e), ': ', $e->getMessage(), "<br>";
//.........这里部分代码省略.........
示例14: function
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function () use($config) {
return new DbAdapter($config->toArray());
});
$di->set('mailgun', function () use($config) {
return $config->mailgun;
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();
});
/**
* Start the session the first time some component request the session service
*/
$di->setShared('session', function () {
$session = new SessionAdapter();
//check if session hasnt already started
if ($session->session_id != "") {
$session->start();
}
return $session;
});
/**
* Setup flash messages for alerts
*/
$di->set('flash', function () {
return new \Phalcon\Flash\Session(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-warning'));
}, true);
示例15: function
/**
* The URL component is used to generate all kinds of URLs in the application
*/
$di->set('url', function () {
$url = new UrlResolver();
$url->setBaseUri('/@@name@@/');
return $url;
});
/**
* Setting up the view component
*/
$di->setShared('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_'));
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function () use($config) {
return new DbAdapter($config->database->toArray());
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();