/** * Cacheable methods * * @param MethodInvocation $invocation Invocation * * @Go\Lang\Annotation\Around("execution(public Nucleus\Cache\BaseCacheService->set(*))") */ public function arroundSet(MethodInvocation $invocation) { $arguments = $invocation->getArguments(); $call = array('method' => 'set', 'name' => $arguments[0], 'namespace' => $arguments[3]); $this->getDebugBar()->getCollector('messages')->addMessage('Cache set [' . $arguments[3] . '] / [' . $arguments[0] . ']'); $result = $invocation->proceed(); $this->data['calls'][] = $call; return $result; }
/** * @Go\After("execution(public **->__construct(*))") * * @param MethodInvocation $constructorInvocation * * @return mixed * * @throws \ErrorException|\Exception */ public function postConstruct(MethodInvocation $constructorInvocation) { $that = $constructorInvocation->getThis(); $scope = $constructorInvocation->getMethod()->getDeclaringClass()->getName(); array_map(function (callable $checker) use($that, $scope) { $checker($that, $scope); }, $this->stateCheckers); return $constructorInvocation->proceed(); }
/** * After throwing exception invoker * * @param $invocation MethodInvocation the method invocation joinpoint * @return mixed the result of the call to {@link Joinpoint::proceed()} * @throws Exception */ public final function invoke(MethodInvocation $invocation) { $result = null; try { $result = $invocation->proceed(); } catch (Exception $invocationException) { $adviceMethod = $this->adviceMethod; $adviceMethod($invocation); throw $invocationException; } return $result; }
/** * This method will be called BEFORE "CommentHelper::render()". * * @param \Go\Aop\Intercept\MethodInvocation $invocation Invocation * @Around("execution(public Comment\View\Helper\CommentHelper->render(*))") */ public function beforeCommentsRender(MethodInvocation $invocation) { $helper = $invocation->getThis(); $view = $this->getProperty($helper, '_View'); if ($helper->config('visibility') > 0) { $settings = plugin('Disqus')->settings; if (!empty($settings['disqus_shortname'])) { return $view->element('Disqus.js', ['settings' => $settings]); } return '<!-- Disqus plugin not configured -->'; } return $invocation->proceed(); }
/** * Cacheable methods * * @param MethodInvocation $invocation Invocation * * @Go\Lang\Annotation\Around("execution(public Twig_Template->display(*))") */ public function aroundDisplay(MethodInvocation $invocation) { $this->deepness++; $start = microtime(true); $result = $invocation->proceed(); $end = microtime(true); if (!is_null($timeDataCollector = $this->getTimeDataCollector())) { $name = sprintf("twig.render(%s)", $invocation->getThis()->getTemplateName()); $timeDataCollector->addMeasure($name, $start, $end); } $this->renderedTemplates[] = array('name' => str_repeat('-', $this->deepness) . '> ' . $invocation->getThis()->getTemplateName(), 'render_time' => $end - $start); $this->deepness--; return $result; }
/** * After invoker * * @param $invocation MethodInvocation the method invocation joinpoint * @return mixed the result of the call to {@link Joinpoint::proceed()} * @throws Exception */ public final function invoke(MethodInvocation $invocation) { $result = null; try { $result = $invocation->proceed(); } catch (Exception $invocationException) { // this is need for finally emulation in PHP } $adviceMethod = $this->adviceMethod; $adviceMethod($invocation); if (isset($invocationException)) { throw $invocationException; } return $result; }
/** * This advice intercepts an execution of cacheable methods * * Logic is pretty simple: we look for the value in the cache and if it's not present here * then invoke original method and store it's result in the cache. * * Real-life examples will use APC or Memcache to store value in the cache * * @param MethodInvocation $invocation Invocation * * @Around("@execution(Demo\Annotation\Cacheable)") */ public function aroundCacheable(MethodInvocation $invocation) { static $memoryCache = array(); $time = microtime(true); $obj = $invocation->getThis(); $class = is_object($obj) ? get_class($obj) : $obj; $key = $class . ':' . $invocation->getMethod()->name; if (!isset($memoryCache[$key])) { // We can use ttl value from annotation, but Doctrine annotations doesn't work under GAE if (!isset($_SERVER['APPENGINE_RUNTIME'])) { echo "Ttl is: ", $invocation->getMethod()->getAnnotation('Demo\\Annotation\\Cacheable')->time, PHP_EOL; } $memoryCache[$key] = $invocation->proceed(); } echo "Take ", sprintf("%0.3f", (microtime(true) - $time) * 1000.0), "ms to call method {$key}", PHP_EOL; return $memoryCache[$key]; }
/** * @param MethodInvocation $invocation Invocation * @Around("@annotation(Reynholm\Aop\Annotation\Transactional)") * * @return mixed */ public function aroundTransactional(MethodInvocation $invocation) { /** @var Transactional $transactionalAnnotation */ $transactionalAnnotation = $invocation->getMethod()->getAnnotation('Reynholm\\LaravelAop\\Annotation\\Transactional'); if (empty($transactionalAnnotation->databaseConnection)) { $transactionalAnnotation->databaseConnection = \Config::get('database.default'); } /** @var \PDO $pdoConnection */ $pdoConnection = \DB::connection($transactionalAnnotation->databaseConnection)->getPdo(); try { $pdoConnection->beginTransaction(); $result = $invocation->proceed(); $pdoConnection->commit(); return $result; } catch (\Exception $e) { $pdoConnection->rollBack(); if ($transactionalAnnotation->throwExceptions === true) { throw $e; } } }
/** * This method will check, if user is authorized * * @param MethodInvocation $invocation * @Around("$this->authorizePointcut") */ public function authorize(MethodInvocation $invocation) { $rflMethod = $invocation->getMethod(); $type = null; $userFactory = null; $expression = null; $resourceFactory = null; $resourceFactoryAdditionalParameters = null; $policies = array(); foreach ($rflMethod->getAnnotations() as $annotation) { switch ($annotation) { case $annotation instanceof AuthorizationSecurity: $type = $annotation->securityTypeName(); $userFactory = $annotation->userFactoryName(); break; case $annotation instanceof AuthorizationExpression: $expression = $annotation->expression(); break; case $annotation instanceof AuthorizationResourceFactory: $resourceFactory = $annotation->resourceFactoryName(); $resourceFactoryAdditionalParameters = $annotation->additionalParameters(); break; case $annotation instanceof AuthorizationPolicy: $policies[] = $annotation->policyName(); break; } } /** @var Security $securityAPI */ $securityAPI = DIContainer::getInstance()->get('security'); if (!is_null($resourceFactory)) { /** @var ResourceFactory $userFactory */ $resourceFactoryForArguments = DIContainer::getInstance()->getResourceFactory($resourceFactory); $resourceFactoryForArguments->setArguments($invocation->getArguments()); $resourceFactoryForArguments->setAdditionalParameters($resourceFactoryAdditionalParameters); } $securityCommand = new SecurityCommand($type, $userFactory, $expression, $resourceFactory, $policies); $securityAPI->authorize($securityCommand); $invocation->proceed(); }
/** * @param MethodInvocation $invocation Invocation * * @Go\Lang\Annotation\Around("execution(public Kohana_Request_Client_Internal->execute_request(*))") */ public function aroundKohanaRequestClientInternalExecuteRequest(MethodInvocation $invocation) { $arguments = $invocation->getArguments(); $kohanaRequest = $arguments[0]; /* @var $kohanaRequest \Request */ $kohanaParameters = $kohanaRequest->param(); $request = $this->createNucleusRequest($kohanaRequest, $kohanaParameters); //We do this now so a controller in kohana can generate route properly from nucleus $this->getRouter()->setCurrentRequest($request); if (!isset($kohanaParameters['_nucleus'])) { return $invocation->proceed(); } $previousKohanaRequest = KohanaRequest::$current; KohanaRequest::$current = $kohanaRequest; $request->request->add($kohanaParameters['_nucleus']); $service = $request->request->get('_service'); $response = $this->getFrontController()->execute($service['name'], $service['method'], $request); $kohanaResponse = $kohanaRequest->create_response(); $this->mergeResponse($kohanaResponse, $response); KohanaRequest::$current = $previousKohanaRequest; return $kohanaResponse; }
/** * Fluent interface advice * * @Around("within(Demo\Aspect\FluentInterface+) && execution(public **->set*(*))") * * @param MethodInvocation $invocation * @return mixed|null|object */ protected function aroundMethodExecution(MethodInvocation $invocation) { $result = $invocation->proceed(); return $result !== null ? $result : $invocation->getThis(); }
/** * @param MethodInvocation $methodInvocation * * @return void * * @throws \ErrorException */ public function __invoke(MethodInvocation $methodInvocation) { $reflectionMethod = $methodInvocation->getMethod(); $applyTypeChecks = $this->applyTypeChecks; $applyTypeChecks($this->getReturnDocblockType($methodInvocation->getMethod()->getDeclaringClass()->getName(), $reflectionMethod), $methodInvocation->proceed()); }
/** * Implement this method to perform extra treatments before and * after the invocation. Polite implementations would certainly * like to invoke {@link Joinpoint::proceed()}. * * @param MethodInvocation $invocation the method invocation joinpoint * @return mixed the result of the call to {@link Joinpoint::proceed()}, * might be intercepted by the interceptor. */ public function invoke(MethodInvocation $invocation) { $this->adviceMethod->__invoke($invocation->getThis(), $invocation->getMethod(), $this->message, $this->level); return $invocation->proceed(); }