示例#1
0
 /**
  * @param ExceptionEvent $event
  */
 public function onException(ExceptionEvent $event)
 {
     $exception = $event->getException();
     $content = new Json();
     $content->setJson(['file' => str_replace(APP_ROOT_PATH, '', $exception->getFile()), 'line' => $exception->getLine(), 'message' => $exception->getMessage()]);
     $this->getResponse()->setStatusCode(500)->setContent($content);
 }
示例#2
0
 /**
  * @param RouteEvent $event
  */
 public function onRoute(RouteEvent $event)
 {
     // Prepare gallery output
     $gallery = ['images' => []];
     // Get request data
     $data = $this->getRequest()->getData();
     // Requested page
     $page = empty($data['p']) ? 0 : (int) $data['p'];
     $pagePattern = empty($data['pp']) ? '' : $data['pp'];
     // Has requested page and page pattern
     if (!empty($page) && !empty($pagePattern)) {
         // Set image pattern
         $this->getGallerizer()->getImagePatternExtractor()->setPattern(empty($data['ip']) ? '' : $data['ip']);
         // Set page pattern
         $this->getGallerizer()->getPagePatternExtractor()->setPattern($pagePattern);
         // Dependency injection tolerated in route observer only :)
         $url = new Http(str_replace('@page@', $page, $pagePattern));
         // Get images
         $images = $this->getGallerizer()->gallerize($url)->getImages();
         /**
          * For each images
          *
          * @var Image $image
          */
         foreach ($images as $image) {
             $gallery['images'][] = ['src' => $image->getSrc(), 'href' => $image->getLink()->getUrl()];
         }
     }
     $content = new Json();
     $content->setJson($gallery['images']);
     $this->getResponse()->setContent($content)->setHeader('X-Memory', sprintf('%sMB', round(memory_get_peak_usage(true) / 1024 / 1024, 2)));
 }
示例#3
0
 /**
  * @param RouteEvent $event
  */
 public function onRoute(RouteEvent $event)
 {
     $content = new Json();
     $content->setJson(['message' => sprintf($this->getMessage(), $event->getRoute()->getParam('name'))]);
     $this->getResponse()->setContent($content);
 }
示例#4
0
 /**
  * @param RouteEvent $event
  */
 public function onRoute(RouteEvent $event)
 {
     // Get request data
     $data = $this->getRequest()->getData();
     // Dependency injection tolerated in route observer only :)
     $url = new Http($data['url']);
     // Gallerize the URL
     $gallerizer = $this->getGallerizer()->gallerize($url);
     // Get images
     $images = $gallerizer->getImages();
     // Default page
     $page = 1;
     // Get page pattern
     $pagePattern = $this->getGallerizer()->getPagePatternExtractor()->getPattern();
     if ($pagePattern) {
         // Prepare reg exp
         $pagePattern = preg_quote($pagePattern, '/');
         $pagePattern = str_replace('@page@', '([0-9]+)', $pagePattern);
         $pagePattern = sprintf('/^%s$/i', $pagePattern);
         // Extract page number
         preg_match($pagePattern, $url->getUrl(), $matches);
         // Found page
         if (empty($matches[1]) === false && (int) $matches[1] > 1) {
             $page = (int) $matches[1];
         }
     }
     // Prepare gallery output
     $gallery = ['images' => [], 'pattern' => ['image' => $gallerizer->getImagePatternExtractor()->getPattern(), 'page' => $gallerizer->getPagePatternExtractor()->getPattern()], 'page' => $page];
     /**
      * For each images
      *
      * @var Image $image
      */
     foreach ($images as $image) {
         $gallery['images'][] = ['src' => $image->getSrc(), 'href' => $image->getLink()->getUrl()];
     }
     // Set response's content
     $content = new Json();
     $content->setJson($gallery);
     $this->getResponse()->setContent($content)->setHeader('X-Memory', sprintf('%sMB', round(memory_get_peak_usage(true) / 1024 / 1024, 2)));
 }