本文整理汇总了PHP中Phalcon\Events\Manager::attach方法的典型用法代码示例。如果您正苦于以下问题:PHP Manager::attach方法的具体用法?PHP Manager::attach怎么用?PHP Manager::attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\Events\Manager
的用法示例。
在下文中一共展示了Manager::attach方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: factory
/**
* Create view instance.
* If no events manager provided - events would not be attached.
*
* @param DIBehaviour $di DI.
* @param Config $config Configuration.
* @param string|null $viewsDirectory Views directory location.
* @param Manager|null $em Events manager.
*
* @return View
*/
public static function factory($di, $config, $viewsDirectory = null, $em = null)
{
$view = new View();
$volt = new Volt($view, $di);
$volt->setOptions(["compiledPath" => $config->application->view->compiledPath, "compiledExtension" => $config->application->view->compiledExtension, 'compiledSeparator' => $config->application->view->compiledSeparator, 'compileAlways' => $config->application->debug && $config->application->view->compileAlways]);
$compiler = $volt->getCompiler();
$compiler->addExtension(new Extension());
$view->registerEngines([".volt" => $volt])->setRenderLevel(View::LEVEL_ACTION_VIEW)->restoreViewDir();
if (!$viewsDirectory) {
$view->setViewsDir($viewsDirectory);
}
// Attach a listener for type "view".
if ($em) {
$em->attach("view", function ($event, $view) use($di, $config) {
if ($config->application->profiler && $di->has('profiler')) {
if ($event->getType() == 'beforeRender') {
$di->get('profiler')->start();
}
if ($event->getType() == 'afterRender') {
$di->get('profiler')->stop($view->getActiveRenderPath(), 'view');
}
}
if ($event->getType() == 'notFoundView') {
throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"');
}
});
$view->setEventsManager($em);
}
return $view;
}
示例2: registerDbprofiler
/**
* 注册Profiler,监听DB
* @param $dblist 监听的db列表,默认监听db
* @return [type] [description]
*/
public static function registerDbprofiler($dblist = array('db'))
{
if (DI::getDefault()['request']->hasQuery('debug') && APP_ENV != 'product') {
$profiler = new Profiler();
$di = DI::getDefault();
$di->setShared('profiler', function () use($profiler) {
return $profiler;
});
foreach ($dblist as $value) {
$eventsManager = new Manager();
$connection = $di[$value];
$eventsManager->attach('db', function ($event, $connection) use($profiler) {
//一条语句查询之前事件,profiler开始记录sql语句
if ($event->getType() == 'beforeQuery') {
$profiler->startProfile($connection->getSQLStatement());
}
//一条语句查询结束,结束本次记录,记录结果会保存在profiler对象中
if ($event->getType() == 'afterQuery') {
$profiler->stopProfile();
}
});
//将事件管理器绑定到db实例中
$connection->setEventsManager($eventsManager);
}
}
}
示例3: setup
/**
* Prepares component
*
* @param stdClass $database
*/
public static function setup($database)
{
if (!isset($database->adapter)) {
throw new \Phalcon\Exception('Unspecified database Adapter in your configuration!');
}
$adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $database->adapter;
if (!class_exists($adapter)) {
throw new \Phalcon\Exception('Invalid database Adapter!');
}
$configArray = $database->toArray();
unset($configArray['adapter']);
self::$_connection = new $adapter($configArray);
self::$_databaseConfig = $database;
$profiler = new Profiler();
$eventsManager = new EventsManager();
$eventsManager->attach('db', function ($event, $connection) use($profiler) {
if ($event->getType() == 'beforeQuery') {
$profiler->startProfile($connection->getSQLStatement());
}
if ($event->getType() == 'afterQuery') {
$profiler->stopProfile();
}
});
self::$_connection->setEventsManager($eventsManager);
}
示例4: testDbLoggerDefault
/**
* Tests the creation of the log file
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2012-11-30
*/
public function testDbLoggerDefault()
{
$this->populateTable('customers', 10);
$fileName = $this->getFileName('log', 'log');
$evman = new PhEventsManager();
$listener = new Listener(PATH_LOGS . $fileName);
$evman->attach('db', $listener);
$connection = $this->di->get('db');
$connection->setEventsManager($evman);
$connection->query("SELECT * FROM customers LIMIT 3");
$connection->query("SELECT * FROM customers LIMIT 10");
$connection->query("SELECT * FROM customers LIMIT 1");
$this->assertTrue($connection->close());
$listener->getLogger()->close();
$lines = file(PATH_LOGS . $fileName);
$this->assertEquals(count($lines), 3);
$this->assertTrue(strpos($lines[0], '[DEBUG]') !== false);
$this->assertTrue(strpos($lines[0], 'LIMIT 3') !== false);
$this->assertTrue(strpos($lines[1], '[DEBUG]') !== false);
$this->assertTrue(strpos($lines[1], 'LIMIT 10') !== false);
$this->assertTrue(strpos($lines[2], '[DEBUG]') !== false);
$this->assertTrue(strpos($lines[2], 'LIMIT 1') !== false);
$this->cleanFile(PATH_LOGS, $fileName);
//$this->assertTrue($actual, 'File was not correctly created');
}
示例5: boot
public function boot()
{
$app = di()->get('application');
$event_manager = new EventsManager();
$event_manager->attach('application', new ApplicationEventListener());
$app->setEventsManager($event_manager);
}
示例6: registerServices
public function registerServices($di)
{
/** Read configuration */
$config = (include __DIR__ . "/../configs/config.php");
$di['dispatcher'] = function () use($di) {
$eventsManager = new EventsManager();
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setDefaultNamespace("Modules\\Users\\Controllers");
$eventsManager->attach('dispatch:beforeException', new NotFoundPlugin($di));
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
};
/** Setting up the view component */
$di['view'] = function () {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir(__DIR__ . '/views/');
$view->setLayoutsDir('../../common/layouts/');
$view->setPartialsDir('../../common/partials/');
$view->setTemplateAfter('users');
return $view;
};
/** Database connection is created based in the parameters defined in the configuration file */
$di['db'] = function () use($config) {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name, "charset" => "utf8", "options" => array(\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")));
};
}
示例7: __construct
/**
* Initialize Phalcon .
*/
public function __construct()
{
$executionTime = -microtime(true);
define('APP_PATH', realpath('..') . '/');
$this->config = $config = new ConfigIni(APP_PATH . 'app/config/config.ini');
$this->di = new FactoryDefault();
$this->di->set('config', $config);
$this->di->set('dispatcher', function () {
$eventsManager = new EventsManager();
$eventsManager->attach('dispatch:beforeExecuteRoute', new SecurityPlugin());
$dispatcher = new Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
$this->di->set('crypt', function () use($config) {
$crypt = new Crypt();
$crypt->setKey($config->application->phalconCryptKey);
return $crypt;
});
$this->setDisplayErrors();
$this->title = $config->application->title;
$this->registerDirs();
$this->setDB($config);
$this->setViewProvider($config);
$this->setURLProvider($config);
$this->setSession();
$this->application = new Application($this->di);
$this->application->view->executionTime = $executionTime;
$this->setCSSCollection();
$this->setJSCollection();
$this->setTitle();
}
示例8: boot
public function boot()
{
$dispatcher = di()->get('dispatcher');
$event_manager = new EventsManager();
$event_manager->attach('dispatch', new DispatcherEventListener());
$dispatcher->setEventsManager($event_manager);
}
示例9: factory
/**
* Create view instance.
* If no events manager provided - events would not be attached.
*
* @param DIBehaviour $di DI.
* @param Config $config Configuration.
* @param string $viewsDirectory Views directory location.
* @param Manager|null $em Events manager.
*
* @return View
*/
public static function factory($di, $config, $viewsDirectory, $em = null)
{
$view = new PhView();
$volt = new PhVolt($view, $di);
$volt->setOptions(["compiledPath" => $config->global->view->compiledPath, "compiledExtension" => $config->global->view->compiledExtension, 'compiledSeparator' => $config->global->view->compiledSeparator, 'compileAlways' => $config->global->view->compileAlways]);
$compiler = $volt->getCompiler();
$compiler->addExtension(new EnViewExtension());
$compiler->addFilter('floor', 'floor');
$compiler->addFunction('range', 'range');
$compiler->addFunction('in_array', 'in_array');
$compiler->addFunction('count', 'count');
$compiler->addFunction('str_repeat', 'str_repeat');
$view->registerEngines(['.volt' => $volt])->setRenderLevel(PhView::LEVEL_ACTION_VIEW)->setViewsDir($viewsDirectory);
// Attach a listener for type "view".
if ($em) {
$em->attach("view", function ($event, $view) use($di, $config) {
if ($config->global->profiler && $di->has('profiler')) {
if ($event->getType() == 'beforeRender') {
$di->get('profiler')->start();
}
if ($event->getType() == 'afterRender') {
$di->get('profiler')->stop($view->getActiveRenderPath(), 'view');
}
}
if ($event->getType() == 'notFoundView') {
throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"');
}
});
$view->setEventsManager($em);
}
$di->set('view', $view);
return $view;
}
示例10: registerServices
/**
* Registers the module-only services
*
* @param Phalcon\DI $di
*/
public function registerServices($di)
{
/**
* Read configuration
*/
$config = (include __DIR__ . "/config/config.php");
$di['view']->setViewsDir(__DIR__ . '/views/');
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
$connection = new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname));
$eventsManager = new EventsManager();
$logger = new FileLogger(__DIR__ . "/logs/db.log");
//Listen all the database events
$eventsManager->attach('db:beforeQuery', function ($event, $connection) use($logger) {
$sqlVariables = $connection->getSQLVariables();
if (count($sqlVariables)) {
$logger->log($connection->getSQLStatement() . ' ' . join(', ', $sqlVariables), Logger::INFO);
} else {
$logger->log($connection->getSQLStatement(), Logger::INFO);
}
});
//Assign the eventsManager to the db adapter instance
$connection->setEventsManager($eventsManager);
return $connection;
};
}
示例11: 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;
});
}
示例12: registerServices
/**
* Registers the module-only services
*
* @param Phalcon\DI $di
*/
public function registerServices(\Phalcon\DiInterface $di = NULL)
{
/**
* Read configuration
*/
$config = (include __DIR__ . "/config/config.php");
$authConfig = (include __DIR__ . "/config/authConfig.php");
/**
* Setting up the view component
*/
$di->set('dispatcher', function () {
$eventsManager = new EventsManager();
$eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
//Handle 404 exceptions
if ($exception instanceof DispatchException) {
$dispatcher->forward(array('controller' => 'public', 'action' => 'error404'));
return false;
}
//Alternative way, controller or action doesn't exist
if ($event->getType() == 'beforeException') {
switch ($exception->getCode()) {
case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(array('controller' => 'public', 'action' => 'error404'));
return false;
}
}
});
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
$dispatcher->setDefaultNamespace("Mall\\Mall\\Controllers");
return $dispatcher;
});
$di['view'] = function () {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
return $view;
};
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
return new Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "options" => array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'", \PDO::ATTR_CASE => \PDO::CASE_LOWER, \PDO::ATTR_EMULATE_PREPARES => false)));
};
$di->set('cookies', function () {
$cookies = new \Phalcon\Http\Response\Cookies();
$cookies->useEncryption(false);
return $cookies;
});
$di->set('authConfig', function () use($authConfig) {
return $authConfig;
});
$di->set('casLoginUrl', function () {
$config = (include __DIR__ . "/../utils/cas/config/webpcConfig.php");
$backurl = $config['domain'] . '/index/vali?forward=' . $config['domain'] . $_SERVER['REQUEST_URI'];
return $config['loginUrl'] . '?siteid=' . $config['siteid'] . '&backurl=' . urlencode($backurl);
});
}
示例13: registerEvents
/**
* Register all event listener from config.
*
* @return mixed
*/
public function registerEvents()
{
$listeners = $this->config['event'];
foreach ($listeners as $name => $listener) {
$listener = '\\' . $listener;
$this->eventManager->attach($name, new $listener());
}
return;
}
示例14: registerServices
/**
* Registers services related to the module
*
* @param DiInterface $dependencyInjector
*/
public function registerServices(DiInterface $dependencyInjector)
{
/**
* Read configuration
*/
$config = (include __DIR__ . "/config/config.php");
/**
* Registering a dispatcher
*/
$dependencyInjector->set('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace('Frontend\\Controllers');
/**
* Not-found action or handler
*/
$eventsManager = new EventsManager();
$eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_CYCLIC_ROUTING:
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(['controller' => 'about', 'action' => 'error']);
return false;
}
});
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
/**
* Setting up the view component
*/
$dependencyInjector->set('view', function () {
$view = new View();
$view->registerEngines(array('.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
$view->setViewsDir(__DIR__ . '/views/');
return $view;
});
$dependencyInjector->set('viewCache', function () use($config) {
//Cache data for one day by default
$frontCache = new OutputFrontend(array("lifetime" => 86400));
//File connection settings
$cache = new FileBackend($frontCache, array('cacheDir' => STATIC_PATH . '/'));
return $cache;
});
$dependencyInjector->set('cookies', function () {
$cookies = new Cookies();
$cookies->useEncryption(false);
return $cookies;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$dependencyInjector->set('db', function () use($config) {
return new DbAdapter($config->database->toArray());
});
}
示例15: registerServices
/**
* Registers the module-only services
*
* @param Phalcon\DI $di
*/
public function registerServices(\Phalcon\DiInterface $di = NULL)
{
/**
* Read configuration
*/
$config = (include __DIR__ . "/config/config.php");
$sdkconfig = (include __DIR__ . "/config/sdkConfig.php");
/**
* Setting up the view component
*/
$di['view'] = function () {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
return $view;
};
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
return new Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "options" => array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'", \PDO::ATTR_CASE => \PDO::CASE_LOWER)));
};
/**
* Setting up the view component
*/
$di->set('dispatcher', function () {
$eventsManager = new EventsManager();
$eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
//Handle 404 exceptions
if ($exception instanceof DispatchException) {
$dispatcher->forward(array('controller' => 'public', 'action' => 'error404'));
return false;
}
//Alternative way, controller or action doesn't exist
if ($event->getType() == 'beforeException') {
switch ($exception->getCode()) {
case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(array('controller' => 'public', 'action' => 'error404'));
return false;
}
}
});
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
$dispatcher->setDefaultNamespace("Ucenter\\Webpc\\Controllers");
return $dispatcher;
});
$di['sdkconfig'] = function () use($sdkconfig) {
return $sdkconfig;
};
$di->set('cookies', function () {
$cookies = new \Phalcon\Http\Response\Cookies();
$cookies->useEncryption(false);
return $cookies;
});
}