コード例 #1
0
 /**
  * @param LoggableAnnotate $annotation
  * @param MethodInvocation $invocation
  *
  * @return string[]
  */
 protected function logFormatter(LoggableAnnotate $annotation, MethodInvocation $invocation)
 {
     $context = [];
     $arguments = $invocation->getArguments();
     foreach ($invocation->getMethod()->getParameters() as $parameter) {
         $context['args'][$parameter->name] = $arguments[$parameter->getPosition()];
     }
     return ['level' => $annotation->value, 'message' => sprintf($this->format, $annotation->name, $invocation->getMethod()->class, $invocation->getMethod()->name), 'context' => $context];
 }
コード例 #2
0
 public function invoke(MethodInvocation $invocation)
 {
     $resource = $invocation->getThis();
     $result = $invocation->proceed();
     $annotation = $this->reader->getMethodAnnotation($invocation->getMethod(), Annotation\ResourceDelegate::class);
     if (isset($annotation)) {
         $class = $this->getDelegateClassName($annotation, $resource);
         if (!class_exists($class)) {
             throw new InvalidAnnotationException('Resource Delegate class is not found.');
         }
         $method = isset($resource->uri->query['_override']) ? $resource->uri->query['_override'] : $resource->uri->method;
         if (stripos($method, $resource->uri->method) !== 0) {
             throw new InvalidMatcherException('Overriden method must match to original method');
         }
         $call = $this->resolveDelegateMethod($method);
         if (!method_exists($class, $call)) {
             throw new InvalidMatcherException('Resource Delegate method is not found');
         }
         $delegate = new $class($resource);
         $params = $this->paramHandler->getParameters([$delegate, $call], $resource->uri->query);
         return call_user_func_array([$delegate, $call], $params);
     } else {
         $result;
     }
 }
コード例 #3
0
 /**
  * @param MethodInvocation $invocation
  *
  * @return object
  * @throws \Exception
  */
 public function invoke(MethodInvocation $invocation)
 {
     /** @var \Illuminate\Database\ConnectionInterface $database */
     $annotation = $invocation->getMethod()->getAnnotation($this->annotation);
     $connection = $annotation->value;
     $database = self::$databaseManager->connection($connection);
     $database->beginTransaction();
     try {
         $result = $invocation->proceed();
         $database->commit();
     } catch (\Exception $exception) {
         // for default Exception
         if ($exception instanceof QueryException) {
             $database->rollBack();
             throw $exception;
         }
         if ($exception instanceof $annotation->expect) {
             $database->rollBack();
             throw $exception;
         }
         $database->rollBack();
         throw $exception;
     }
     return $result;
 }
コード例 #4
0
ファイル: UserValidator.php プロジェクト: mackstar/spout
 public function invoke(MethodInvocation $invocation)
 {
     (array) ($args = $invocation->getArguments());
     $validator = $this->validator;
     $method = $invocation->getMethod()->name;
     $id = $method == 'onPut' ? $args[self::ID] : null;
     if (!$validator->get('emailaddress')->isValid($args[self::EMAIL])) {
         $this->errors['email'] = $validator->getMessages()[0];
     }
     if (!$validator->get('notempty')->isValid($args[self::NAME])) {
         $this->errors['name'] = $validator->getMessages()[0];
     }
     if (!$validator->get('notempty')->isValid($args[self::ROLE]['id'])) {
         $this->errors['role'] = $validator->getMessages()[0];
     }
     if ($method == 'onPost' && !$validator->get('notempty')->isValid($args[self::PASSWORD])) {
         $this->errors['password'] = $validator->getMessages()[0];
     }
     if (!isset($this->errors['email']) && !$this->isUniqueEmail($args[self::EMAIL], $id)) {
         $this->errors['email'] = 'Email address already exists.';
     }
     if (implode('', $this->errors) == '') {
         return $invocation->proceed();
     }
     return $this->resource->get->uri('app://spout/exceptions/validation')->withQuery(['errors' => $this->errors])->eager->request();
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  */
 public function invoke(MethodInvocation $invocation)
 {
     $annotation = $this->extractAnnotation($invocation->getMethod(), Transactional::class);
     $scope = new TransactionScope(new DbalTransaction($this->conn, $annotation), $annotation);
     return $scope->runInto(function () use($invocation) {
         return $invocation->proceed();
     });
 }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 public function get(MethodInvocation $invocation)
 {
     $args = $invocation->getArguments()->getArrayCopy();
     $params = $invocation->getMethod()->getParameters();
     $namedArgs = [];
     foreach ($params as $param) {
         if (isset($namedArgs[$param->name])) {
             throw new DuplicatedNamedParam($param->name);
         }
         $namedArgs[$param->name] = array_shift($args);
     }
     return $namedArgs;
 }
コード例 #7
0
 /**
  * @param MethodInvocation|\Ray\Aop\ReflectiveMethodInvocation $invocation
  *
  * @return mixed
  */
 public function invoke(MethodInvocation $invocation)
 {
     $annotation = $invocation->getMethod()->getAnnotation($this->annotation);
     $keys = $this->generateCacheName($annotation->cacheName, $invocation);
     if (!is_array($annotation->key)) {
         $annotation->key = [$annotation->key];
     }
     $keys = $this->detectCacheKeys($invocation, $annotation, $keys);
     // detect use cache driver
     $cache = $this->detectCacheRepository($annotation);
     $result = $invocation->proceed();
     if ($result !== null) {
         $cache->put($this->recursiveImplode($this->join, $keys), $result, $annotation->lifetime);
     }
     return $result;
 }
コード例 #8
0
 /**
  * @param MethodInvocation $invocation
  *
  * @return mixed
  */
 public function invoke(MethodInvocation $invocation)
 {
     $annotation = $invocation->getMethod()->getAnnotation($this->annotation);
     $keys = $this->generateCacheName($annotation->cacheName, $invocation);
     if (!is_array($annotation->key)) {
         $annotation->key = [$annotation->key];
     }
     $keys = $this->detectCacheKeys($invocation, $annotation, $keys);
     // detect use cache driver
     $cache = $this->detectCacheRepository($annotation);
     if ($annotation->allEntries) {
         $cache->flush();
         return $invocation->proceed();
     }
     $result = $invocation->proceed();
     $cache->forget($this->recursiveImplode($this->join, $keys));
     return $result;
 }
コード例 #9
0
 /**
  * @param MethodInvocation $invocation
  * @param                  $annotation
  * @param                  $keys
  *
  * @return array
  */
 protected function detectCacheKeys(MethodInvocation $invocation, $annotation, $keys)
 {
     $arguments = $invocation->getArguments();
     foreach ($invocation->getMethod()->getParameters() as $parameter) {
         // exclude object
         if (in_array('#' . $parameter->name, $annotation->key)) {
             if (isset($arguments[$parameter->getPosition()])) {
                 if (!is_object($arguments[$parameter->getPosition()])) {
                     $keys[] = $arguments[$parameter->getPosition()];
                 }
             }
             if (!isset($arguments[$parameter->getPosition()])) {
                 $keys[] = $parameter->getDefaultValue();
             }
         }
     }
     return $keys;
 }
コード例 #10
0
 /**
  * @param MethodInvocation $invocation
  *
  * @return object
  * @throws \Exception
  */
 public function invoke(MethodInvocation $invocation)
 {
     /** @var \Ytake\LaravelAspect\Annotation\Loggable $annotation */
     $annotation = $invocation->getMethod()->getAnnotation($this->annotation);
     $start = microtime(true);
     $result = $invocation->proceed();
     $time = microtime(true) - $start;
     $logFormat = $this->logFormatter($annotation, $invocation);
     $logger = self::$logger;
     if ($logger instanceof Writer) {
         $logger = $logger->getMonolog();
     }
     if (!$annotation->skipResult) {
         $logFormat['context']['result'] = $result;
     }
     $logFormat['context']['time'] = $time;
     /** Monolog\Logger */
     $logger->log($logFormat['level'], $logFormat['message'], $logFormat['context']);
     return $result;
 }
コード例 #11
0
 /**
  * Intercepts any method and injects instances of the missing arguments
  * when they are type hinted
  */
 public function invoke(MethodInvocation $invocation)
 {
     $object = $invocation->getThis();
     $parameters = $invocation->getMethod()->getParameters();
     $arguments = $invocation->getArguments()->getArrayCopy();
     $assisted = [];
     foreach ($parameters as $k => $p) {
         $hint = $p->getClass();
         if ($hint) {
             $assisted[$k] = PipingBag::get($hint->getName());
             continue;
         }
         if (isset($arguments[$k])) {
             $assisted[$k] = array_shift($arguments);
             continue;
         }
         $assisted[$k] = $p->getDefaultValue();
     }
     $invocation->getArguments()->exchangeArray($assisted);
     return $invocation->proceed();
 }
コード例 #12
0
ファイル: ResourceValidator.php プロジェクト: mackstar/spout
 public function invoke(MethodInvocation $invocation)
 {
     (array) ($args = $invocation->getArguments());
     $validator = $this->validator;
     $method = $invocation->getMethod()->name;
     $id = $method == 'onPut' ? $args[self::ID] : null;
     if (!$validator->get('notempty')->isValid($args[self::TITLE])) {
         $this->errors['title'] = $validator->getMessages()[0];
     }
     if (!$validator->get('notempty')->isValid($args[self::TYPE]['id'])) {
         $this->errors['type'] = $validator->getMessages()[0];
     }
     if (!$validator->get('notempty')->isValid($args[self::TYPE]['id'])) {
         $this->errors['type'] = $validator->getMessages()[0];
     }
     if (empty($this->errors) && !$this->isUniqueResourceName($args[self::TYPE]['slug'], $args[self::SLUG], $id)) {
         $this->errors['slug'] = 'The slug has already been taken for this media type.';
     }
     if (implode('', $this->errors) == '') {
         return $invocation->proceed();
     }
     return $this->resource->get->uri('app://spout/exceptions/validation')->withQuery(['errors' => $this->errors])->eager->request();
 }
コード例 #13
0
 /**
  * @param MethodInvocation $invocation
  *
  * @return object
  * @throws \Exception
  */
 public function invoke(MethodInvocation $invocation)
 {
     /** @var \Ytake\LaravelAspect\Annotation\LogExceptions $annotation */
     $annotation = $invocation->getMethod()->getAnnotation($this->annotation);
     try {
         $result = $invocation->proceed();
     } catch (\Exception $exception) {
         if ($exception instanceof $annotation->expect) {
             $logFormat = $this->logFormatter($annotation, $invocation);
             $logger = self::$logger;
             if ($logger instanceof Writer) {
                 $logger = $logger->getMonolog();
             }
             /** Monolog\Logger */
             $logFormat['context']['code'] = $exception->getCode();
             $logFormat['context']['error_message'] = $exception->getMessage();
             $logger->log($logFormat['level'], $logFormat['message'], $logFormat['context']);
         }
         throw $exception;
     }
     // @codeCoverageIgnoreStart
     return $result;
     // @codeCoverageIgnoreEnd
 }
コード例 #14
0
 /**
  * @param MethodInvocation $invocation
  *
  * @return object
  * @throws \Exception
  */
 public function invoke(MethodInvocation $invocation)
 {
     /** @var Async $annotation */
     $annotation = $invocation->getMethod()->getAnnotation($this->annotation);
     $stack = [];
     for ($i = 1; $i <= $annotation->process; $i++) {
         $pid = pcntl_fork();
         if ($pid === -1) {
             throw new \RuntimeException('pcntl_fork() returned -1');
         } elseif ($pid) {
             $stack[$pid] = true;
             if (count($stack) >= $annotation->process) {
                 unset($stack[pcntl_waitpid(-1, $status, WUNTRACED)]);
             }
             return null;
         } else {
             $invocation->proceed();
             exit;
         }
     }
     while (count($stack) > 0) {
         unset($stack[pcntl_waitpid(-1, $status, WUNTRACED)]);
     }
 }
コード例 #15
0
ファイル: DbInjector.php プロジェクト: mackstar/spout
 /**
  * {@inheritdoc}
  */
 public function invoke(MethodInvocation $invocation)
 {
     $object = $invocation->getThis();
     $method = $invocation->getMethod();
     $write = ['onPut', 'onDelete', 'onPost'];
     $connectionParams = in_array($method->name, $write) ? $this->masterDb : $this->slaveDb;
     $pagerAnnotation = $this->reader->getMethodAnnotation($method, 'BEAR\\Sunday\\Annotation\\DbPager');
     $db = $this->getDb($pagerAnnotation, $connectionParams);
     /* @var $db \BEAR\Package\Module\Database\Dbal\PagerConnection */
     if ($this->sqlLogger instanceof SQLLogger) {
         $db->getConfiguration()->setSQLLogger($this->sqlLogger);
     }
     $object->setDb($db);
     $result = $invocation->proceed();
     if ($this->sqlLogger instanceof DebugStack) {
         $this->sqlLogger->stopQuery();
         $object->headers['x-sql'] = [$this->sqlLogger->queries];
     } elseif ($this->sqlLogger instanceof SQLLogger) {
         $this->sqlLogger->stopQuery();
     }
     if (!$pagerAnnotation) {
         return $result;
     }
     $pagerData = $db->getPager();
     if ($pagerData) {
         $object->headers['pager'] = $pagerData;
     }
     return $result;
 }
コード例 #16
0
 public function invoke(MethodInvocation $invocation)
 {
     throw new RuntimeException($invocation->getMethod()->getName() . " not allowed every day!");
 }
コード例 #17
0
ファイル: DbInjector.php プロジェクト: rsky/BEAR.Package
 /**
  * (non-PHPdoc)
  * @see Ray\Aop.MethodInterceptor::invoke()
  */
 public function invoke(MethodInvocation $invocation)
 {
     $object = $invocation->getThis();
     $method = $invocation->getMethod();
     $connectionParams = $method->name !== 'onGet' ? $this->slaveDb : $this->masterDb;
     $pagerAnnotation = $this->reader->getMethodAnnotation($method, 'BEAR\\Sunday\\Annotation\\DbPager');
     if ($pagerAnnotation) {
         $connectionParams['wrapperClass'] = 'BEAR\\Package\\Module\\Database\\DoctrineDbalModule\\Connection';
         $db = DriverManager::getConnection($connectionParams);
         $db->setMaxPerPage($pagerAnnotation->limit);
     } else {
         $db = DriverManager::getConnection($connectionParams);
     }
     /* @var $db \BEAR\Package\Module\Database\DoctrineDbalModule\Connection */
     if ($this->sqlLogger instanceof SQLLogger) {
         $db->getConfiguration()->setSQLLogger($this->sqlLogger);
     }
     $object->setDb($db);
     $result = $invocation->proceed();
     if ($this->sqlLogger instanceof SQLLogger) {
         $this->sqlLogger->stopQuery();
         $object->headers['x-sql'] = [$this->sqlLogger->queries];
     }
     if ($pagerAnnotation) {
         $pagerData = $db->getPager();
         if ($pagerData) {
             $object->headers['pager'] = $pagerData;
         }
     }
     return $result;
 }