/** * (non-PHPdoc) * @see Ray\Aop.MethodInterceptor::invoke() */ public function invoke(MethodInvocation $invocation) { // retrieve page and query $args = $invocation->getArguments(); $page = $invocation->getThis(); // strip tags foreach ($args as &$arg) { $arg = strip_tags($arg); } // required title if ($args[self::TITLE] === '') { $this->errors['title'] = 'title required.'; } // required body if ($args[self::BODY] === '') { $this->errors['body'] = 'body required.'; } // valid form ? if (implode('', $this->errors) === '') { return $invocation->proceed(); } // error, modify 'GET' page wih error message. $page['errors'] = $this->errors; $page['submit'] = ['title' => $args[self::TITLE], 'body' => $args[self::BODY]]; return $page->onGet(); }
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; } }
public function invoke(MethodInvocation $invocation) { $user = $this->session->get('user'); $object = $invocation->getThis(); $object->setUserId($user['id']); return $invocation->proceed(); }
/** * @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; }
public function invoke(MethodInvocation $invocation) { $response = $invocation->proceed(); $this->removeFromSingleRecord($response->body); $this->removeFromArrayRecord($response->body); return $response; }
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(); }
/** * {@inheritdoc} */ public function invoke(MethodInvocation $invocation) { $object = $invocation->getThis(); $auth = $this->auth->start(); $object->body['auth'] = $auth; $result = $invocation->proceed(); return $result; }
/** * {@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(); }); }
public function invoke(MethodInvocation $invocation) { $object = $invocation->getThis(); $user = $this->session->get('user'); if ($this->isAccessDenied($object->uri, $user)) { return $this->resource->get->uri('app://spout/exceptions/accessdenied')->eager->request(); } return $invocation->proceed(); }
/** * @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]; }
/** * {@inheritdoc} */ public function invoke(MethodInvocation $invocation) { $session = $this->factory->newInstance($_COOKIE); $csrfToken = $session->getCsrfToken(); if (!$csrfToken->isValid($this->getActualToken())) { throw new InvalidCsrfException(); } $csrfToken->regenerateValue(); return $invocation->proceed(); }
public function invoke(MethodInvocation $invocation) { $response = $invocation->proceed(); if (isset($response->headers['pager'])) { unset($response->headers['pager']['html']); $response->body['_pager'] = $response->headers['pager']; unset($response->headers['pager']); } return $response; }
/** * (non-PHPdoc) * @see Ray\Aop.MethodInterceptor::invoke() */ public function invoke(MethodInvocation $invocation) { if (is_writable($this->tmpDir)) { return $invocation->proceed(); } $pageObject = $invocation->getThis(); /** @noinspection PhpUnusedLocalVariableInspection */ $tmpDir = $this->tmpDir; $pageObject->view = (include __DIR__ . '/Checker/error.php'); return $pageObject; }
/** * {@inheritdoc} */ public function invoke(MethodInvocation $invocation) { $object = $invocation->getThis(); if (!method_exists($object, 'getDbConnection')) { throw new \RuntimeException('The object needs to implement getDbConnection'); } $object->getDbConnection()->transactional(function () use(&$result, $invocation) { $result = $invocation->proceed(); }); return $result; }
/** * {@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; }
/** * (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; }
/** * @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; }
/** * @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; }
/** * @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; }
/** * @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; }
public function invoke(MethodInvocation $invocation) { $response = $invocation->proceed(); $path = parse_url($response->uri)['path']; $names = explode('/', $path); $i = count($names); $name = $names[$i - 1]; if ($name === 'index' || $name === 'search') { $name = $names[$i - 2]; } if (isset($response->body[$name])) { $modelName = $name; } else { $singular = $this->string->singularize($name); if ($name != $singular && isset($response->body[$singular])) { $modelName = $singular; } } if (isset($modelName)) { $response->body['_model'] = $modelName; } return $response; }
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(); }
/** * @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 }
/** * @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)]); } }
/** * 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(); }
/** * {@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; }
public function invoke(MethodInvocation $invocation) { $response = $invocation->proceed(); $response->body['_user'] = $this->session->get('user'); return $response; }
public function invoke(MethodInvocation $invocation) { throw new RuntimeException($invocation->getMethod()->getName() . " not allowed every day!"); }
public function invoke(MethodInvocation $invocation) { $result = $invocation->proceed(); file_put_contents(sprintf('%s/var/log/resource.%d.log', dirname(dirname(__DIR__)), date('Ymd')), sprintf("[%s]\n%s\n\n", date('H:i:s'), var_export(['resource' => (array) $invocation->getThis()->uri, 'result' => $result, 'body' => file_get_contents('php://input'), 'server' => $_SERVER, 'get' => $_GET, 'post' => $_POST], true)), FILE_APPEND); return $result; }
/** * (non-PHPdoc) * @see Ray\Aop.MethodInterceptor::invoke() */ public function invoke(MethodInvocation $invocation) { $time = date('g:i'); $result = $invocation->proceed() . ". It is {$time} now !"; return $result; }