public function __invoke(RoutingContextInterface $context)
 {
     $match = $context->getRouteMatch();
     $identifier = (string) $match->getParameter('identifier');
     $pipeline = $this->manager->findPipeline($identifier);
     $pipeHash = $pipeline->getHash();
     $etag = new EntityTag($pipeHash);
     $ttl = $pipeline->getTtl();
     $expires = new \DateTimeImmutable('@' . (time() + $ttl));
     $response = $context->getRequest()->evaluatePreconditions($etag);
     if ($response !== NULL) {
         $response->setHeader('Cache-Control', sprintf('public, max-age=%u', $ttl));
         $response->setHeader(new ExpiresHeader($expires));
         return $response;
     }
     if (is_dir($this->cachePath)) {
         foreach (glob($this->cachePath . '/' . $identifier . '-*', GLOB_NOSORT) as $file) {
             @unlink($file);
         }
     }
     $response = new HttpResponse();
     $response->setHeader('Content-Type', sprintf('%s; charset="%s"', $pipeline->getMediaType(), $pipeline->getEncoding()));
     $response->setHeader('Access-Control-Allow-Origin', '*');
     $response->setHeader('Cache-Control', sprintf('public, max-age=%u', $ttl));
     $response->setHeader('ETag', $etag);
     $response->setHeader(new ExpiresHeader($expires));
     if (is_dir($this->cachePath)) {
         $file = $this->cachePath . '/' . $identifier . '-' . $pipeHash;
         Filesystem::writeFile($file, $pipeline->dump($this->publisher));
         $response->setEntity(new FileEntity(new \SplFileInfo($file)));
     } else {
         $response->setEntity((string) $pipeline->dump($this->publisher));
     }
     return $response;
 }
 public function readEntity(RoutingContextInterface $context, \ReflectionClass $ref, &$isRead)
 {
     if ($ref->name !== JsonEntity::class) {
         return;
     }
     $isRead = true;
     $request = $context->getRequest();
     return new JsonEntity(@json_decode($request->getContents(), true));
 }
 public function readEntity(RoutingContextInterface $context, \ReflectionClass $ref, &$isRead)
 {
     if ($ref->name !== \DOMDocument::class) {
         return;
     }
     $request = $context->getRequest();
     if (!$request->hasEntity()) {
         return;
     }
     $isRead = true;
     $builder = new XmlDocumentBuilder();
     return $builder->buildFromSource($request->getEntity()->getContents());
 }
 public function readEntity(RoutingContextInterface $context, \ReflectionClass $ref, &$isRead)
 {
     if (!$ref->implementsInterface(EntityInterface::class)) {
         return;
     }
     $request = $context->getRequest();
     if (!$request->hasEntity()) {
         return;
     }
     $entity = $request->getEntity();
     if ($ref->isInstance($entity)) {
         $isRead = true;
         return $entity;
     }
 }
 /**
  * Get the HTTP request (shortcut to the context).
  * 
  * @var HttpRequest
  */
 public function getRequest()
 {
     return $this->context->getRequest();
 }
 /**
  * Execute WebDAV controller logic.
  * 
  * @param RoutingContextInterface $context
  * @return HttpResponse
  */
 public function __invoke(RoutingContextInterface $context)
 {
     return $this->invokeHandlers($context->getPathInfo(), $context->getBaseUri(), $context->getRequest());
 }
Esempio n. 7
0
 public function transformActionArguments(\ReflectionFunctionAbstract $ref, RoutingContextInterface $context)
 {
     $request = $context->getRequest();
     $match = $context->getRouteMatch();
     $instances = [$context, $request, $match];
     $args = [];
     $resolverNeeded = [];
     // Merge query params with route params, route params have preceedence.
     $params = Arrays::mergeDeep($request->getUri()->getQuery(), $match->getParameters());
     foreach ($ref->getParameters() as $i => $param) {
         $paramType = $param->getClass();
         if ($paramType) {
             $isRead = false;
             foreach ($this->plugins->getEntityReaders() as $reader) {
                 $result = $reader->readEntity($context, $paramType, $isRead);
                 if ($isRead) {
                     $args[$i] = $result;
                     continue 2;
                 }
             }
         }
         if (array_key_exists($param->name, $params)) {
             $value = $params[$param->name];
             $isResolved = false;
             foreach ($this->plugins->getValueConverters() as $converter) {
                 $result = $converter->resolveInput($value, $context, $isResolved, $paramType);
                 if ($isResolved) {
                     $args[$i] = $result;
                     continue 2;
                 }
             }
             if ($paramType !== NULL) {
                 $resolverNeeded[$i] = $param;
                 continue;
             }
             $args[$i] = $value;
             continue;
         }
         if ($paramType === NULL) {
             if (!$param->isOptional()) {
                 throw new \InvalidArgumentException(sprintf('Missing value of parameter "%s"', $param->name));
             }
             $args[$i] = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : NULL;
             continue;
         }
         foreach ($instances as $instance) {
             if ($paramType->isInstance($instance)) {
                 $args[$i] = $instance;
                 continue 2;
             }
         }
         $resolverNeeded[$i] = $param;
     }
     foreach ($resolverNeeded as $i => $param) {
         $paramType = $param->getClass();
         $isResolved = false;
         foreach ($this->plugins->getValueResolvers() as $resolver) {
             $result = $resolver->resolve($args, $context, $isResolved, $paramType);
             if ($isResolved) {
                 $args[$i] = $result;
                 continue 2;
             }
         }
         $args[$i] = $this->get($param);
     }
     ksort($args, SORT_NUMERIC);
     return $args;
 }