本文整理匯總了PHP中CakeEventManager類的典型用法代碼示例。如果您正苦於以下問題:PHP CakeEventManager類的具體用法?PHP CakeEventManager怎麽用?PHP CakeEventManager使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了CakeEventManager類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: work
public function work(GearmanJob $job)
{
$workload = $job->workload();
$json = json_decode($workload, true);
if (!json_last_error()) {
$workload = $json;
}
$eventManager = new CakeEventManager();
$eventManager->dispatch(new CakeEvent('Gearman.beforeWork', $this, $workload));
$data = call_user_func($this->_workers[$job->functionName()], $job, $workload);
$eventManager->dispatch(new CakeEvent('Gearman.afterWork', $this, $workload));
return $data;
}
示例2: testProcessVersion
/**
* testProcessVersion
*
* @return void
*/
public function testProcessVersion()
{
$this->Image->create();
$result = $this->Image->save(array('foreign_key' => 'test-1', 'model' => 'Test', 'file' => array('name' => 'titus.jpg', 'size' => 332643, 'tmp_name' => CakePlugin::path('FileStorage') . DS . 'Test' . DS . 'Fixture' . DS . 'File' . DS . 'titus.jpg', 'error' => 0)));
$result = $this->Image->find('first', array('conditions' => array('id' => $this->Image->getLastInsertId())));
$this->assertTrue(!empty($result) && is_array($result));
$this->assertTrue(file_exists($this->testPath . $result['ImageStorage']['path']));
$path = $this->testPath . $result['ImageStorage']['path'];
$Folder = new Folder($path);
$folderResult = $Folder->read();
$this->assertEqual(count($folderResult[1]), 3);
Configure::write('Media.imageSizes.Test', array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200))));
ClassRegistry::init('FileStorage.ImageStorage')->generateHashes();
$Event = new CakeEvent('ImageVersion.createVersion', $this->Image, array('record' => $result, 'storage' => StorageManager::adapter('Local'), 'operations' => array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200)))));
CakeEventManager::instance()->dispatch($Event);
$path = $this->testPath . $result['ImageStorage']['path'];
$Folder = new Folder($path);
$folderResult = $Folder->read();
$this->assertEqual(count($folderResult[1]), 4);
$Event = new CakeEvent('ImageVersion.removeVersion', $this->Image, array('record' => $result, 'storage' => StorageManager::adapter('Local'), 'operations' => array('t200' => array('thumbnail' => array('mode' => 'outbound', 'width' => 200, 'height' => 200)))));
CakeEventManager::instance()->dispatch($Event);
$path = $this->testPath . $result['ImageStorage']['path'];
$Folder = new Folder($path);
$folderResult = $Folder->read();
$this->assertEqual(count($folderResult[1]), 3);
}
示例3: changeStatus
public function changeStatus(Model $Model, $status, $id = null, $force = false)
{
if ($id === null) {
$id = $Model->getID();
}
if ($id === false) {
return false;
}
$force = true;
$Model->id = $id;
if (!$Model->exists()) {
throw new NotFoundException();
}
if ($force !== true) {
$modelData = $Model->read();
if ($modelData[$Model->alias]['status'] === $status) {
CakeLog::write(LOG_WARNING, __d('webshop', 'The status of %1$s with id %2$d is already set to %3$s. Not making a change', strtolower(Inflector::humanize(Inflector::underscore($Model->name))), $id, $status), array('webshop'));
return false;
}
} else {
CakeLog::write(LOG_WARNING, __d('webshop', 'Status change of %1$s with id %2$d is being forced to %3$s', strtolower(Inflector::humanize(Inflector::underscore($Model->name))), $id, $status), array('webshop'));
}
$Model->saveField('status', $status);
CakeLog::write(LOG_INFO, __d('webshop', 'Changed status of %1$s with id %2$d to %3$s', strtolower(Inflector::humanize(Inflector::underscore($Model->name))), $id, $status), array('webshop'));
$eventData = array();
$eventData[Inflector::underscore($Model->name)]['id'] = $id;
$eventData[Inflector::underscore($Model->name)]['status'] = $status;
$overallEvent = new CakeEvent($Model->name . '.statusChanged', $this, $eventData);
$specificEvent = new CakeEvent($Model->name . '.statusChangedTo' . Inflector::camelize($status), $this, $eventData);
CakeEventManager::instance()->dispatch($overallEvent);
CakeEventManager::instance()->dispatch($specificEvent);
return true;
}
示例4: import
/**
* Import exchange rate for the euro.
*/
public function import()
{
$exchangeRateImporter = new EcbExchangeRateImporter();
$exchangeRateImporter->import();
$event = new CakeEvent('ExchangeRateImport.completed', $this);
CakeEventManager::instance()->dispatch($event);
}
示例5: onProductBought
public function onProductBought($event)
{
$this->Product->id = $event->data['product']['id'];
$this->Product->recursive = 2;
$product = $this->Product->read();
if ($product[$this->Product->WebhostingProduct->alias]['id'] === null) {
return;
}
$this->Product->WebhostingProduct->HostGroup->id = $product['HostGroup']['id'];
$this->Product->WebhostingProduct->HostGroup->recursive = 2;
$hostGroup = $this->Product->WebhostingProduct->HostGroup->read();
$WebhostingProvider = BasicWebhostingProvider::get($hostGroup['Host'][0]['Provider']['class']);
$webhostingPackage = $this->WebhostingPackage->createFromWebhostingProduct($product['WebhostingProduct'], $event->data['customer']['id'], $hostGroup['Host'][0]['id']);
$webhostingDetails = $WebhostingProvider->createPackage($webhostingPackage['WebhostingPackage']['id']);
if ($webhostingDetails === false) {
CakeLog::write(LOG_ERROR, __d('pltfrm', 'Webhosting provider %1$s could not create webhosting package with id %2$d owned by %3$s', $hostGroup['Host'][0]['Provider']['class'], $webhostingPackage['WebhostingPackage']['id'], $webhostingPackage['Customer']['name']), array('webhosting', 'pltfrm'));
return false;
}
$eventData = array();
$eventData['webhosting']['id'] = $webhostingPackage['WebhostingPackage']['id'];
$eventData['details'] = $webhostingDetails;
$eventData['metadata'] = array();
$webhostingCreatedEvent = new CakeEvent('Webhosting.created', $this, $eventData);
CakeEventManager::instance()->dispatch($webhostingCreatedEvent);
CakeLog::write(LOG_INFO, __d('pltfrm', 'Webhosting provider %1$s created webhosting package with id %2$d', $hostGroup['Host'][0]['Provider']['class'], $webhostingPackage['WebhostingPackage']['id']), array('webhosting', 'pltfrm'));
if (isset($event->data['order'])) {
$this->OrderProduct->changeStatus('delivered', $event->data['order']['product_id']);
}
return true;
}
示例6: dispatchEvent
/**
* Dispatch event
*
* @param string $name Name of the event
* @param object $subject the object that this event applies to (usually the object that is generating the event)
* @param mixed $data any value you wish to be transported with this event to it can be read by listeners
*/
public static function dispatchEvent($name, $subject = null, $data = [])
{
$event = new CakeEvent($name, $subject, $data);
if (!$subject) {
CakeEventManager::instance()->dispatch($event);
} else {
$subject->getEventManager()->dispatch($event);
}
}
示例7: __construct
/**
* Constructor
* Overridden to provide Twig loading
*
* @param Controller $Controller Controller
*/
public function __construct(Controller $Controller = null)
{
$this->Twig = new Twig_Environment(new Twig_Loader_Cakephp(array()), array('cache' => Configure::read('TwigView.Cache'), 'charset' => strtolower(Configure::read('App.encoding')), 'auto_reload' => Configure::read('debug') > 0, 'autoescape' => false, 'debug' => Configure::read('debug') > 0));
CakeEventManager::instance()->dispatch(new CakeEvent('Twig.TwigView.construct', $this, array('TwigEnvironment' => $this->Twig)));
parent::__construct($Controller);
if (isset($Controller->theme)) {
$this->theme = $Controller->theme;
}
$this->ext = self::EXT;
}
示例8: initialize
/**
* Default settings
*
* @var array
*/
public function initialize(Controller $Controller)
{
$this->settings = array_merge($this->_defaultSettings, $this->settings);
$this->Controller = $Controller;
$this->_EventManager = CakeEventManager::instance();
if (empty($this->settings['model'])) {
$this->settings['model'] = $this->Controller->modelClass;
}
$this->sessionKey = $this->settings['sessionKey'];
}
示例9: instance
/**
* Returns the globally available instance of a CakeEventManager
* this is used for dispatching events attached from outside the scope
* other managers were created. Usually for creating hook systems or inter-class
* communication
*
* If called with a first params, it will be set as the globally available instance
*
* @param CakeEventManager $manager
* @return CakeEventManager the global event manager
*/
public static function instance($manager = null)
{
if ($manager instanceof CakeEventManager) {
self::$_generalManager = $manager;
}
if (empty(self::$_generalManager)) {
self::$_generalManager = new CakeEventManager();
}
self::$_generalManager->_isGlobal = true;
return self::$_generalManager;
}
示例10: start
/**
* Starts the websocket server
*
* @return void
*/
public function start()
{
$this->__loop = LoopFactory::create();
if ($this->__loop instanceof \React\EventLoop\StreamSelectLoop) {
$this->out('<warning>Your configuration doesn\'t seem to support \'ext-libevent\'. It is highly reccomended that you install and configure it as it provides significant performance gains over stream select!</warning>');
}
$socket = new Reactor($this->__loop);
$socket->listen(Configure::read('Ratchet.Connection.websocket.port'), Configure::read('Ratchet.Connection.websocket.address'));
$this->__ioServer = new IoServer(new HttpServer(new WsServer(new SessionProvider(new WampServer(new CakeWampAppServer($this, $this->__loop, CakeEventManager::instance(), $this->params['verbose'])), new CakeWampSessionHandler(), [], new PhpSerializeHandler()))), $socket, $this->__loop);
$this->__loop->run();
}
示例11: _detachAllListeners
/**
* Detaches all listeners from the Cart events to avoid application level events changing the tests
*
* @return void
*/
protected function _detachAllListeners()
{
$EventManager = CakeEventManager::instance();
$events = array('Cart.beforeCalculateCart', 'Cart.applyTaxRules', 'Cart.applyDiscounts', 'Cart.afterCalculateCart');
foreach ($events as $event) {
$listeners = $EventManager->listeners($event);
foreach ($listeners as $listener) {
foreach ($listener['callable'] as $callable) {
$EventManager->detach($callable);
}
}
}
}
示例12: testConstruct
public function testConstruct()
{
$this->_hibernateListeners('Twig.TwigView.construct');
$callbackFired = false;
$that = $this;
$eventCallback = function ($event) use($that, &$callbackFired) {
$that->assertInstanceof('Twig_Environment', $event->data['TwigEnvironment']);
$callbackFired = true;
};
CakeEventManager::instance()->attach($eventCallback, 'Twig.TwigView.construct');
$TwigView = new TwigView();
CakeEventManager::instance()->detach($eventCallback, 'Twig.TwigView.construct');
$this->_wakeupListeners('Twig.TwigView.construct');
$this->assertTrue($callbackFired);
}
示例13: imageUrl
/**
* URL
*
* @param array $image FileStorage array record or whatever else table that matches this helpers needs without the model, we just want the record fields
* @param string $version Image version string
* @param array $options HtmlHelper::image(), 2nd arg options array
* @throws InvalidArgumentException
* @return string
*/
public function imageUrl($image, $version = null, $options = array())
{
if (empty($image) || empty($image['id'])) {
return false;
}
if (!empty($version)) {
$hash = Configure::read('Media.imageHashes.' . $image['model'] . '.' . $version);
if (empty($hash)) {
throw new \InvalidArgumentException(__d('file_storage', 'No valid version key (%s %s) passed!', @$image['model'], $version));
}
} else {
$hash = null;
}
$Event = new CakeEvent('FileStorage.ImageHelper.imagePath', $this, array('hash' => $hash, 'image' => $image, 'version' => $version, 'options' => $options));
CakeEventManager::instance()->dispatch($Event);
if ($Event->isStopped()) {
return $this->normalizePath($Event->data['path']);
} else {
return false;
}
}
示例14: _expectedEventCalls
protected function _expectedEventCalls(&$asserts, $events)
{
$cbi = [];
foreach ($events as $eventName => $event) {
$this->__preservedEventListeners[$eventName] = CakeEventManager::instance()->listeners($eventName);
foreach ($this->__preservedEventListeners[$eventName] as $eventListener) {
$this->eventManager->detach($eventListener['callable'], $eventName);
}
$count = count($events[$eventName]['callback']);
for ($i = 0; $i < $count; $i++) {
$asserts[$eventName . '_' . $i] = false;
}
$cbi[$eventName] = 0;
$this->eventManager->attach(function ($event) use(&$events, $eventName, &$asserts, &$cbi) {
$asserts[$eventName . '_' . $cbi[$eventName]] = true;
call_user_func($events[$eventName]['callback'][$cbi[$eventName]], $event);
$cbi[$eventName]++;
}, $eventName);
}
return $asserts;
}
示例15: callback
/**
* Default callback entry point for API callbacks for payment processors
*
* @param null $token
* @throws NotFoundException
* @internal param string $processor
* @return void
*/
public function callback($token = null)
{
$order = $this->Order->find('first', array('contain' => array(), 'conditions' => array('Order.token' => $token)));
if (empty($order)) {
throw new NotFoundException(__d('cart', 'Invalid payment token %s!', $token));
}
try {
$Processor = $this->_loadPaymentProcessor($order['Order']['processor']);
$status = $Processor->notificationCallback($order);
$transactionId = $Processor->getTransactionId();
if (empty($order['Order']['payment_reference']) && !empty($transactionId)) {
$order['Order']['payment_reference'] = $transactionId;
}
$result = $this->Order->save(array('Order' => array('id' => $order['Order']['id'], 'payment_status' => $status, 'payment_reference' => $order['Order']['payment_reference'])), array('validate' => false, 'callbacks' => false));
} catch (Exception $e) {
$this->log($e->getMessage(), 'payment-error');
$this->log($this->request, 'payment-error');
}
$Event = new CakeEvent('Payment.callback', $this->request);
CakeEventManager::dispatch($Event, $this, array($result));
$this->_stop();
}