/** * @param AccessInterceptorInterface $proxy * @param EventManager $eventManager */ public function addEventManagement(AccessInterceptorInterface $proxy) { $proxy->setMethodPrefixInterceptor('find', function ($proxy, $instance, $method, $params, &$returnEarly) { $results = $this->eventManager->trigger('preFind', $instance); if ($results->stopped()) { $returnEarly = true; return $results->last(); } }); $proxy->setMethodSuffixInterceptor('find', function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) { $this->eventManager->trigger('postFind', $instance, ['row' => $returnValue]); }); $proxy->setMethodPrefixInterceptor('insert', function ($proxy, $instance, $method, $params, &$returnEarly) { $returnEarly = true; $this->eventManager->trigger('preInsert', $instance, ['values' => &$params['values']]); return $instance->insert($params['values']); }); $proxy->setMethodPrefixInterceptor('update', function ($proxy, $instance, $method, $params, &$returnEarly) { $returnEarly = true; $this->eventManager->trigger('preUpdate', $instance, ['values' => &$params['values']]); return $instance->update($params['values'], $params['id']); }); $proxy->setMethodSuffixInterceptor('update', function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) { $this->eventManager->trigger('onSave', $instance, ['row' => $returnValue]); }); $proxy->setMethodSuffixInterceptor('delete', function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) { $this->eventManager->trigger('onDelete', $instance); }); $proxy->setMethodSuffixInterceptor('getQueryFindAll', function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) { $this->eventManager->trigger('postFindAll', $instance, ['qb' => $returnValue, 'criteria' => $params['criteria']]); }); }
/** * @param AccessInterceptorInterface $proxy * @param EventManager $eventManager */ public function addEventManagement(AccessInterceptorInterface $proxy) { $proxy->setMethodPrefixInterceptor('get', function ($proxy, $instance, $method, $params, &$returnEarly) { $returnEarly = true; try { $resource = $instance->get($params['id']); $this->triggerEvent('postDispatch', $instance, $resource); } catch (ResourceNotFoundException $e) { $instance->response()->status(404); //Not Found } }); $proxy->setMethodPrefixInterceptor('getList', function ($proxy, $instance, $method, $params, &$returnEarly) { $returnEarly = true; try { $resources = $instance->getList(); $this->triggerEvent('postDispatch', $instance, $resources); } catch (PageOutOfRangeException $e) { $instance->response()->status(404); //Not Found } }); $proxy->setMethodPrefixInterceptor('post', function ($proxy, $instance, $method, $params, &$returnEarly) { $returnEarly = true; try { $resource = $instance->post(); $this->triggerEvent('postDispatch', $instance, $resource); } catch (BadRequestParameters $e) { $this->handleBadRequest($instance); } }); $proxy->setMethodPrefixInterceptor('put', function ($proxy, $instance, $method, $params, &$returnEarly) { $returnEarly = true; try { $resource = $instance->put($params['id']); $this->triggerEvent('postDispatch', $instance, $resource); } catch (ResourceNotFoundException $e) { $instance->response()->status(404); //Not Found } catch (BadRequestParameters $e) { $this->handleBadRequest($instance); } }); $proxy->setMethodPrefixInterceptor('delete', function ($proxy, $instance, $method, $params, &$returnEarly) { $returnEarly = true; try { $instance->delete($params['id']); } catch (ResourceNotFoundException $e) { $instance->response()->status(404); //Not Found } }); }
/** * @param AccessInterceptorInterface $proxy * @param string $method * @param AnnotationCollection $annotations */ private function registerPrefixInterceptor(AccessInterceptorInterface $proxy, $method, AnnotationCollection $annotations) { $cacheHandler = $this->cacheHandler; $proxy->setMethodPrefixInterceptor($method, function ($proxy, $instance, $method, $params, &$returnEarly) use($cacheHandler, $annotations) { $interception = new ProxyInterceptionPrefix($instance, $method, $params); if (!($result = $cacheHandler->interceptProxyPrefix($annotations, $interception))) { return null; } $returnEarly = true; return $result; }); }