Skip to content

Commit

Permalink
Applied dispatchEvent() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Sep 10, 2014
1 parent 5f6bb95 commit f57db21
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 39 deletions.
12 changes: 5 additions & 7 deletions src/Controller/Controller.php
Expand Up @@ -468,11 +468,11 @@ protected function _loadComponents() {
* @return void|\Cake\Network\Response
*/
public function startupProcess() {
$event = $this->eventManager()->dispatch(new Event('Controller.initialize', $this));
$event = $this->dispatchEvent('Controller.initialize');
if ($event->result instanceof Response) {
return $event->result;
}
$event = $this->eventManager()->dispatch(new Event('Controller.startup', $this));
$event = $this->dispatchEvent('Controller.startup');
if ($event->result instanceof Response) {
return $event->result;
}
Expand All @@ -488,7 +488,7 @@ public function startupProcess() {
* @return void|\Cake\Network\Response
*/
public function shutdownProcess() {
$event = $this->eventManager()->dispatch(new Event('Controller.shutdown', $this));
$event = $this->dispatchEvent('Controller.shutdown');
if ($event->result instanceof Response) {
return $event->result;
}
Expand All @@ -512,8 +512,7 @@ public function redirect($url, $status = null) {
$response->statusCode($status);
}

$event = new Event('Controller.beforeRedirect', $this, [$url, $response]);
$event = $this->eventManager()->dispatch($event);
$event = $this->dispatchEvent('Controller.beforeRedirect', [$url, $response]);
if ($event->result instanceof Response) {
return $event->result;
}
Expand Down Expand Up @@ -559,8 +558,7 @@ public function setAction($action) {
* @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
*/
public function render($view = null, $layout = null) {
$event = new Event('Controller.beforeRender', $this);
$event = $this->eventManager()->dispatch($event);
$event = $this->dispatchEvent('Controller.beforeRender');
if ($event->result instanceof Response) {
$this->autoRender = false;
return $event->result;
Expand Down
3 changes: 1 addition & 2 deletions src/Datasource/QueryTrait.php
Expand Up @@ -206,8 +206,7 @@ public function all() {
}

$table = $this->repository();
$event = new Event('Model.beforeFind', $table, [$this, $this->_options, !$this->eagerLoaded()]);
$table->eventManager()->dispatch($event);
$table->dispatchEvent('Model.beforeFind', [$this, $this->_options, !$this->eagerLoaded()]);

if (isset($this->_results)) {
return $this->_results;
Expand Down
3 changes: 1 addition & 2 deletions src/ORM/Association.php
Expand Up @@ -585,8 +585,7 @@ public function deleteAll($conditions) {
protected function _dispatchBeforeFind($query) {
$table = $this->target();
$options = $query->getOptions();
$event = new Event('Model.beforeFind', $table, [$query, $options, false]);
$table->eventManager()->dispatch($event);
$table->dispatchEvent('Model.beforeFind', [$query, $options, false]);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/ORM/EntityValidator.php
Expand Up @@ -138,8 +138,7 @@ protected function _processValidation($entity, $options) {
$type = is_string($options['validate']) ? $options['validate'] : 'default';
$validator = $this->_table->validator($type);
$pass = compact('entity', 'options', 'validator');
$event = new Event('Model.beforeValidate', $this->_table, $pass);
$this->_table->eventManager()->dispatch($event);
$event = $this->_table->dispatchEvent('Model.beforeValidate', $pass);

if ($event->isStopped()) {
return (bool)$event->result;
Expand All @@ -151,8 +150,7 @@ protected function _processValidation($entity, $options) {

$success = $entity->validate($validator);

$event = new Event('Model.afterValidate', $this->_table, $pass);
$this->_table->eventManager()->dispatch($event);
$event = $this->_table->dispatchEvent('Model.afterValidate', $pass);

if ($event->isStopped()) {
$success = (bool)$event->result;
Expand Down
13 changes: 4 additions & 9 deletions src/ORM/Table.php
Expand Up @@ -1180,8 +1180,7 @@ protected function _processSave($entity, $options) {
}

$options['associated'] = $this->_associations->normalizeKeys($associated);
$event = new Event('Model.beforeSave', $this, compact('entity', 'options'));
$this->eventManager()->dispatch($event);
$event = $this->dispatchEvent('Model.beforeSave', compact('entity', 'options'));

if ($event->isStopped()) {
return $event->result;
Expand Down Expand Up @@ -1217,8 +1216,7 @@ protected function _processSave($entity, $options) {
);
if ($success || !$options['atomic']) {
$entity->clean();
$event = new Event('Model.afterSave', $this, compact('entity', 'options'));
$this->eventManager()->dispatch($event);
$this->dispatchEvent('Model.afterSave', compact('entity', 'options'));
$entity->isNew(false);
$entity->source($this->alias());
$success = true;
Expand Down Expand Up @@ -1402,12 +1400,10 @@ public function delete(EntityInterface $entity, $options = []) {
* @return bool success
*/
protected function _processDelete($entity, $options) {
$eventManager = $this->eventManager();
$event = new Event('Model.beforeDelete', $this, [
$event = $this->dispatchEvent('Model.beforeDelete', [
'entity' => $entity,
'options' => $options
]);
$eventManager->dispatch($event);
if ($event->isStopped()) {
return $event->result;
}
Expand All @@ -1434,11 +1430,10 @@ protected function _processDelete($entity, $options) {
return $success;
}

$event = new Event('Model.afterDelete', $this, [
$this->dispatchEvent('Model.afterDelete', [
'entity' => $entity,
'options' => $options
]);
$eventManager->dispatch($event);

return $success;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Routing/Dispatcher.php
Expand Up @@ -58,8 +58,7 @@ class Dispatcher {
* @throws \Cake\Routing\Exception\MissingControllerException When the controller is missing.
*/
public function dispatch(Request $request, Response $response) {
$beforeEvent = new Event('Dispatcher.beforeDispatch', $this, compact('request', 'response'));
$this->eventManager()->dispatch($beforeEvent);
$beforeEvent = $this->dispatchEvent('Dispatcher.beforeDispatch', compact('request', 'response'));

$request = $beforeEvent->data['request'];
if ($beforeEvent->result instanceof Response) {
Expand Down Expand Up @@ -89,8 +88,7 @@ public function dispatch(Request $request, Response $response) {
return $response->body();
}

$afterEvent = new Event('Dispatcher.afterDispatch', $this, compact('request', 'response'));
$this->eventManager()->dispatch($afterEvent);
$afterEvent = $this->dispatchEvent('Dispatcher.afterDispatch', compact('request', 'response'));
$afterEvent->data['response']->send();
}

Expand Down
19 changes: 8 additions & 11 deletions src/View/View.php
Expand Up @@ -416,9 +416,9 @@ public function render($view = null, $layout = null) {

if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
$this->_currentType = static::TYPE_VIEW;
$this->eventManager()->dispatch(new Event('View.beforeRender', $this, array($viewFileName)));
$this->dispatchEvent('View.beforeRender', [$viewFileName]);
$this->Blocks->set('content', $this->_render($viewFileName));
$this->eventManager()->dispatch(new Event('View.afterRender', $this, array($viewFileName)));
$this->dispatchEvent('View.afterRender', [$viewFileName]);
}

if ($layout === null) {
Expand Down Expand Up @@ -451,7 +451,7 @@ public function renderLayout($content, $layout = null) {
} else {
$this->Blocks->set('content', $content);
}
$this->eventManager()->dispatch(new Event('View.beforeLayout', $this, array($layoutFileName)));
$this->dispatchEvent('View.beforeLayout', [$layoutFileName]);

$title = $this->Blocks->get('title');
if ($title === '') {
Expand All @@ -462,7 +462,7 @@ public function renderLayout($content, $layout = null) {
$this->_currentType = static::TYPE_LAYOUT;
$this->Blocks->set('content', $this->_render($layoutFileName));

$this->eventManager()->dispatch(new Event('View.afterLayout', $this, array($layoutFileName)));
$this->dispatchEvent('View.afterLayout', [$layoutFileName]);
return $this->Blocks->get('content');
}

Expand Down Expand Up @@ -696,14 +696,11 @@ protected function _render($viewFile, $data = array()) {
$this->_current = $viewFile;
$initialBlocks = count($this->Blocks->unclosed());

$eventManager = $this->eventManager();
$beforeEvent = new Event('View.beforeRenderFile', $this, array($viewFile));
$this->dispatchEvent('View.beforeRenderFile', [$viewFile]);

$eventManager->dispatch($beforeEvent);
$content = $this->_evaluate($viewFile, $data);

$afterEvent = new Event('View.afterRenderFile', $this, array($viewFile, $content));
$eventManager->dispatch($afterEvent);
$afterEvent = $this->dispatchEvent('View.afterRenderFile', [$viewFile, $content]);
if (isset($afterEvent->result)) {
$content = $afterEvent->result;
}
Expand Down Expand Up @@ -1007,13 +1004,13 @@ protected function _renderElement($file, $data, $options) {
$this->_currentType = static::TYPE_ELEMENT;

if ($options['callbacks']) {
$this->eventManager()->dispatch(new Event('View.beforeRender', $this, array($file)));
$this->dispatchEvent('View.beforeRender', [$file]);
}

$element = $this->_render($file, array_merge($this->viewVars, $data));

if ($options['callbacks']) {
$this->eventManager()->dispatch(new Event('View.afterRender', $this, array($file, $element)));
$this->dispatchEvent('View.afterRender', [$file, $element]);
}

$this->_currentType = $restore;
Expand Down

0 comments on commit f57db21

Please sign in to comment.