getMethod() public méthode

Gets the method being called.
public getMethod ( ) : ReflectionMetho\ReflectionMethod | AnnotatedReflectionMethod
Résultat ReflectionMetho\ReflectionMethod | Go\Aop\Support\AnnotatedReflectionMethod the method being called.
Exemple #1
0
 /**
  * Checks if this method invocation is mockable.
  *
  * @param MethodInvocation $invocation        	
  * @return object The mock return value if this invocation is happening in test
  *         environment and it is listed in the mockable_methods array null otherwise.
  */
 private static function isMockable(MethodInvocation $invocation)
 {
     // Check for testing config, nothing is mockable if we are not testing
     if (Config::get('aopmock.testing') === false) {
         return null;
     }
     // Get the object
     $obj = $invocation->getThis();
     // Get the method name
     $methodName = $invocation->getMethod()->name;
     // Get class name
     if ($invocation->getMethod()->isStatic()) {
         $class = $obj;
         $obj = new $class();
     } else {
         $class = get_class($obj);
     }
     $types = Config::get('aopmock.mockable_methods');
     foreach ($types as $type => $val) {
         if ($obj instanceof $type) {
             if (isset($types[$type][$methodName])) {
                 return $types[$type][$methodName];
             }
         }
     }
     return null;
 }
 /**
  * Method invoker
  *
  * @param $invocation MethodInvocation the method invocation joinpoint
  * @return mixed the result of the call to {@see Joinpoint->proceed()}
  */
 public final function invoke(MethodInvocation $invocation)
 {
     if ($this->pointcut->matches($invocation->getMethod(), $invocation->getThis(), $invocation->getArguments())) {
         return $this->adviceMethod->invoke($invocation);
     }
     return $invocation->proceed();
 }
 /**
  * This advice intercepts an execution of __call methods
  *
  * Unlike traditional "execution" pointcut, "dynamic" is checking the name of method in
  * the runtime, allowing to write interceptors for __call more transparently.
  *
  * @param MethodInvocation $invocation Invocation
  *
  * @Before("dynamic(public Demo\Example\DynamicMethodsDemo->save*(*))")
  */
 public function beforeMagicMethodExecution(MethodInvocation $invocation)
 {
     $obj = $invocation->getThis();
     // we need to unpack args from invocation args
     list($methodName, $args) = $invocation->getArguments();
     echo 'Calling Magic Interceptor for method: ', is_object($obj) ? get_class($obj) : $obj, $invocation->getMethod()->isStatic() ? '::' : '->', $methodName, '()', ' with arguments: ', json_encode($args), PHP_EOL;
 }
 /**
  * @param MethodInvocation $methodInvocation
  *
  * @return void
  *
  * @throws \ErrorException
  */
 public function __invoke(MethodInvocation $methodInvocation)
 {
     $reflectionParameters = $methodInvocation->getMethod()->getParameters();
     $applyTypeChecks = $this->applyTypeChecks;
     foreach ($methodInvocation->getArguments() as $argumentIndex => $argument) {
         $applyTypeChecks($this->getParameterDocblockType(get_class($methodInvocation->getThis()), $reflectionParameters, $argumentIndex), $argument);
     }
 }
 /**
  * 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];
 }
 /**
  * @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();
 }
 /**
  * Returns an associative list of arguments for the method invocation
  *
  * @param MethodInvocation $invocation
  * @return array
  */
 protected function fetchMethodArguments(MethodInvocation $invocation)
 {
     $parameters = $invocation->getMethod()->getParameters();
     $argumentNames = array_map(function (\ReflectionParameter $parameter) {
         return $parameter->name;
     }, $parameters);
     $parameters = array_combine($argumentNames, $invocation->getArguments());
     return $parameters;
 }
Exemple #8
0
 /**
  * Intercept any public method and store them on the object.
  *
  * @param MethodInvocation $invocation Invocation
  * @Before("execution(public Mage_Cms_Block_Page->*(*))")
  */
 public function storeInterceptedPublicMethods(MethodInvocation $invocation)
 {
     $obj = $invocation->getThis();
     $method = $invocation->getMethod()->getName();
     $interceptedMethods = $obj->getInterceptedMethods();
     if ($interceptedMethods === null) {
         $interceptedMethods = array();
     }
     if (!in_array($method, $interceptedMethods)) {
         $interceptedMethods[] = $method;
     }
     $obj->setInterceptedMethods($interceptedMethods);
 }
 /**
  * @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;
         }
     }
 }
Exemple #10
0
 /**
  * @param MethodInvocation $invocation Invocation
  * @Before("execution(public Endpoint->call(*))") // This is our PointCut
  */
 public function beforeServiceCall(MethodInvocation $invocation)
 {
     $methodThis = $invocation->getThis();
     $service = $methodThis->service()->first();
     $serviceName = $service->name;
     $serviceLimit = \DB::table('limits')->where('service_id', '=', $service->id)->first();
     \DB::table('limits')->where('service_id', '=', $service->id)->update(array('current_hits' => 0));
     $serviceLimit->current_hits = 0;
     $hasLimitBeenReached = $serviceLimit->current_hits >= $serviceLimit->max_hits;
     if ($hasLimitBeenReached) {
         echo "The limit number of calls for service \"" . $serviceName . "\" have been reached \n";
         echo "Method execution stoped";
         die;
     }
     \DB::table('limits')->where('service_id', '=', $service->id)->update(array('current_hits' => $serviceLimit->current_hits + 1));
     $methodName = $invocation->getMethod()->getName();
     $methodParams = implode(', ', $invocation->getArguments());
     // echo "Calling method " . $methodName . " with params : " . $methodParams . "\n";
     // echo "Method execution continues \n";
     // echo "Method result \n";
 }
 /**
  * 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();
 }
Exemple #12
0
 protected function getAnnotation(MethodInvocation $invocation, $annotationName)
 {
     return $this->getServiceContainer()->getServiceByName('aspectKernel')->getContainer()->get('aspect.annotation.reader')->getMethodAnnotation($invocation->getMethod(), $annotationName);
 }
 /**
  * @param MethodInvocation $invocation
  * @return array
  */
 private function fetchParentsContracts(MethodInvocation $invocation)
 {
     return $this->methodConditionFetcher->getConditions($invocation->getMethod()->getDeclaringClass(), $invocation->getMethod()->name);
 }
Exemple #14
0
 /**
  * Method that should be called before real method
  *
  * @param MethodInvocation $invocation Invocation
  * @Before("within(**)")
  */
 public function beforeMethodExecution(MethodInvocation $invocation)
 {
     $obj = $invocation->getThis();
     echo 'Calling Before Interceptor for method: ', is_object($obj) ? get_class($obj) : $obj, $invocation->getMethod()->isStatic() ? '::' : '->', $invocation->getMethod()->getName(), '()', ' with arguments: ', json_encode($invocation->getArguments()), "<br>\n";
 }
 /**
  * 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();
 }
 /**
  * @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());
 }
Exemple #17
0
 private function getCacheEntryNameFromString(MethodInvocation $invocation, $name)
 {
     $arguments = $invocation->getArguments();
     $replaces = array();
     foreach ($invocation->getMethod()->getParameters() as $parameter) {
         /* @var $parameter \ReflectionParameter */
         $replaces['$' . $parameter->getName()] = serialize($arguments[$parameter->getPosition()]);
     }
     return str_replace(array_keys($replaces), array_values($replaces), $name);
 }