getRequest() 공개 메소드

Returns always the master request.
public getRequest ( ) : null | Request
리턴 null | Symfony\Component\HttpFoundation\Request
예제 #1
0
 /**
  * Injects all necessary files to get the Jarves Content Editor working
  * on the current page response.
  *
  * This register in `parent.jarves` a new jarves.Editor.
  */
 public function registerEditor()
 {
     $this->addMainResources(['noJs' => true]);
     $this->addSessionScripts();
     $page = $this->pageStack->getCurrentPage();
     $response = $this->pageStack->getPageResponse();
     $response->addJsFile('@JarvesBundle/admin/mootools-core-1.4.5-fixed-memory-leak.js');
     $response->addJsFile('@JarvesBundle/admin/mootools-more.js');
     $response->setResourceCompression(false);
     $response->setDomainHandling(false);
     $request = $this->pageStack->getRequest();
     $nodeArray['id'] = $page->getId();
     $nodeArray['title'] = $page->getTitle();
     $nodeArray['domainId'] = $page->getDomainId();
     $nodeArray['theme'] = $page->getTheme();
     $nodeArray['layout'] = $request->query->get('_jarves_editor_layout') ?: $page->getLayout();
     $domain = DomainQuery::create()->findPk($page->getDomainId());
     $domainArray['id'] = $domain->getId();
     $domainArray['domain'] = $domain->getDomain();
     $domainArray['path'] = $domain->getPath();
     $domainArray['theme'] = $domain->getTheme();
     $domainArray['themeOptions'] = $domain->getThemeOptions();
     $options = ['id' => $request->query->get('_jarves_editor_id'), 'node' => $nodeArray, 'domain' => $domainArray];
     if (is_array($extraOptions = $request->query->get('_jarves_editor_options'))) {
         $options = array_merge($options, $extraOptions);
         $options['standalone'] = filter_var($options['standalone'], FILTER_VALIDATE_BOOLEAN);
     }
     $response->addJsAtBottom('window.editor = new parent.jarves.Editor(' . json_encode($options) . ', document.documentElement);');
 }
예제 #2
0
 /**
  * 
  * Options:
  * 
  *  //whether the template cache is deactivated. Navigation object is still cached
  *  boolean noCache = false
  *
  *  //whether the pathInfo is used in the cacheKey instead of the currentUrl.
  *  //Useful when you have in your navigation controller calls that are based on pathInfo like
  *  //pageStack->getCurrentUrlAffix()
  *  boolean pathInfoCache = false
  * 
  * Example:
  *   getRendered(['noCache' => true]);
  * 
  * @param array $options
  * @param \Twig_Environment $twig
  * @return string
  * @throws \Exception
  */
 public function getRendered($options, \Twig_Environment $twig)
 {
     $options['noCache'] = isset($options['noCache']) ? (bool) $options['noCache'] : false;
     $options['pathInfoCache'] = isset($options['pathInfoCache']) ? (bool) $options['pathInfoCache'] : false;
     $view = $options['template'] ?: $options['view'];
     $cacheKey = 'core/navigation/' . $this->pageStack->getCurrentPage()->getDomainId() . '.' . $this->pageStack->getCurrentPage()->getId() . ($options['pathInfoCache'] ? '_' . md5($this->pageStack->getRequest()->getPathInfo()) : '') . '_' . md5(json_encode($options));
     $fromCache = false;
     $viewPath = $this->jarves->resolvePath($view, 'Resources/views/');
     if ('@' === $view[0]) {
         $view = substr($view, 1);
     }
     if (!file_exists($viewPath)) {
         throw new \Exception(sprintf('View `%s` not found.', $view));
     } else {
         $mtime = filemtime($viewPath);
     }
     if (!$options['noCache']) {
         $cache = $this->cacher->getDistributedCache($cacheKey);
         if ($cache && isset($cache['html']) && $cache['html'] !== null && $cache['mtime'] == $mtime) {
             return $cache['html'];
         }
     }
     $cache = $this->cacher->getDistributedCache($cacheKey);
     if ($cache && isset($cache['object']) && $cache['mtime'] == $mtime) {
         $navigation = unserialize($cache['object']);
         $fromCache = true;
     } else {
         $navigation = $this->get($options);
     }
     $data['navigation'] = $navigation ?: false;
     if ($navigation !== false) {
         $html = $twig->render($view, $data);
         if (!$options['noCache']) {
             $this->cacher->setDistributedCache($cacheKey, array('mtime' => $mtime, 'html' => $html));
         } elseif (!$fromCache) {
             $this->cacher->setDistributedCache($cacheKey, array('mtime' => $mtime, 'object' => serialize($navigation)));
         }
         return $html;
     }
     //no navigation found, probably the template just uses the breadcrumb
     return $twig->render($view, $data);
 }
예제 #3
0
 /**
  * @return LogRequest
  */
 public function getLogRequest()
 {
     if (!$this->logRequest && $this->pageStack->getRequest()) {
         $this->logRequest = new LogRequest();
         $this->logRequest->setId(md5(mt_rand() . ':' . uniqid()));
         $this->logRequest->setDate(microtime(true));
         $this->logRequest->setIp($this->pageStack->getRequest()->getClientIp());
         $this->logRequest->setPath(substr($this->pageStack->getRequest()->getPathInfo(), 0, 254));
         $this->logRequest->setUsername($this->pageStack->getUser() instanceof UserInterface ? $this->pageStack->getUser()->getUsername() : 'Guest');
     }
     return $this->logRequest;
 }
예제 #4
0
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $data = $event->getControllerResult();
     $request = $event->getRequest();
     if (!$event->getRequest()->attributes->get('_jarves_is_plugin')) {
         //we accept only plugin responses.
         return;
     }
     $content = $request->attributes->get('_content');
     if (null !== $data) {
         if ($data instanceof PluginResponseInterface) {
             $response = $data;
         } else {
             $response = $this->pageResponseFactory->createPluginResponse($data);
         }
         //it's required to place a PluginResponseInterface as response, so
         //PluginResponseListener::onKernelResponse can correctly identify it
         //and set it as response for this plugin content, so ContentTypes\TypePlugin
         //can place the correct response at the correct position, without executing
         //the plugin twice.
         $event->setResponse($response);
     } else {
         //we hit a plugin route, but it has responsed with NULL
         //this means it is not responsible for this route/slug.
         //we need now to remove this plugin route from the route collection
         //and fire again a sub request until all plugins on this page
         //are handled. If no plugin is responsible for this url pattern
         //and the main page route is also not responsible
         //no response is set in the $event and a 404 is thrown by the HttpKernel.
         $foundRoute = false;
         $routes = $this->frontendRouteListener->getRoutes();
         foreach ($routes as $idx => $route) {
             /** @var \Symfony\Component\Routing\Route $route */
             if ($content === $route->getDefault('_content')) {
                 //remove exactly only the current plugin that was hit in this sub request
                 $routes->remove($idx);
                 $foundRoute = true;
                 break;
             }
         }
         if ($foundRoute) {
             //we've removed the route and fire now again a sub request
             $request = clone $this->pageStack->getRequest();
             $request->attributes = new ParameterBag();
             $response = $this->kernel->handle($request, HttpKernelInterface::SUB_REQUEST);
             $event->setResponse($response);
         }
         //we do not need to restore routes in the frontendRouteListener, because
         //it reload all routes on every master request
     }
 }
예제 #5
0
 /**
  * @ApiDoc(
  *  section="Backend",
  *  description="Prints all JavaScript files combined"
  * )
  *
  * @Rest\QueryParam(name="printSourceMap", requirements=".+", description="If the sourceMap should printed")
  *
  * @Rest\Get("/admin/backend/script")
  *
  * @return string javascript
  */
 public function loadJsAction()
 {
     $assets = array();
     $md5String = '';
     $newestMTime = 0;
     $jsContent = '';
     foreach ($this->jarves->getConfigs() as $bundleConfig) {
         foreach ($bundleConfig->getAdminAssetsInfo() as $assetInfo) {
             if (!$assetInfo->isJavaScript()) {
                 continue;
             }
             if (!$assetInfo->isCompressionAllowed()) {
                 continue;
             }
             $path = $this->jarves->resolveWebPath($assetInfo->getPath());
             if (file_exists($path)) {
                 $assets[] = $assetInfo->getPath();
                 $mtime = filemtime($path);
                 $newestMTime = max($newestMTime, $mtime);
                 $md5String .= ">{$path}.{$mtime}<";
                 $content = file_get_contents($path);
                 $jsContent .= "\n/* file: {$assetInfo->getPath()} */\n{$content}\n";
             }
         }
     }
     $ifModifiedSince = $this->pageStack->getRequest()->headers->get('If-Modified-Since');
     if (isset($ifModifiedSince) && strtotime($ifModifiedSince) == $newestMTime) {
         // Client's cache IS current, so we just respond '304 Not Modified'.
         $response = new Response();
         $response->setStatusCode(304);
         $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s', $newestMTime) . ' GMT');
         return $response;
     }
     $expires = 60 * 60 * 24 * 14;
     //2 weeks
     $response = new Response();
     $response->headers->set('Content-Type', 'application/javascript');
     $response->headers->set('Pragma', 'public');
     $response->headers->set('Cache-Control', 'max-age=' . $expires);
     $response->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
     //        $content = implode($files);
     $response->setContent($jsContent);
     return $response;
 }
예제 #6
0
 /**
  * @ApiDoc(
  *  section="File Manager",
  *  description="Displays a (complete) image (with cache-headers)"
  * )
  *
  * @Rest\QueryParam(name="path", requirements=".+", strict=true, description="The file path or its ID")
  *
  * @Rest\Get("/admin/file/image")
  *
  * @param ParamFetcher $paramFetcher
  * @return Response
  */
 public function showImageAction(ParamFetcher $paramFetcher)
 {
     $path = $paramFetcher->get('path');
     if (is_numeric($path)) {
         $path = $this->webFilesystem->getPath($path);
     }
     $this->checkAccess($path, ACL::MODE_VIEW);
     $file = $this->webFilesystem->getFile($path);
     if ($file->isDir()) {
         return;
     }
     $ifModifiedSince = $this->pageStack->getRequest()->headers->get('If-Modified-Since');
     if (isset($ifModifiedSince) && strtotime($ifModifiedSince) == $file->getModifiedTime()) {
         // Client's cache IS current, so we just respond '304 Not Modified'.
         $response = new Response();
         $response->setStatusCode(304);
         $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s', $file->getModifiedTime()) . ' GMT');
         return $response;
     }
     $content = $this->webFilesystem->read($path);
     $image = \PHPImageWorkshop\ImageWorkshop::initFromString($content);
     $result = $image->getResult();
     $size = new FileSize();
     $size->setHandleFromBinary($content);
     $expires = 3600;
     //1 h
     $response = new Response();
     $response->headers->set('Content-Type', 'png' == $size->getType() ? 'image/png' : 'image/jpeg');
     $response->headers->set('Pragma', 'public');
     $response->headers->set('Cache-Control', 'max-age=' . $expires);
     $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s', $file->getModifiedTime()) . ' GMT');
     $response->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
     ob_start();
     if ('png' === $size->getType()) {
         imagepng($result, null, 3);
     } else {
         imagejpeg($result, null, 100);
     }
     $response->setContent(ob_get_contents());
     ob_end_clean();
     return $response;
 }
예제 #7
0
 /**
  * Creates a Node object based on given $routeName or current route.
  *
  * @param string|null $routeName
  *
  * @return Node
  */
 public function createPageFromRoute($routeName = null)
 {
     if (!$routeName) {
         $routeName = $this->pageStack->getRequest()->attributes->get('_route');
         if (!$routeName) {
             throw new \RuntimeException('Could not detect route name');
         }
     }
     $reflection = new \ReflectionClass($this->router->getGenerator());
     $key = 'jarves_routes';
     $cache = $this->cacher->getFastCache($key);
     $validCache = false;
     $routes = [];
     if ($cache) {
         $validCache = $cache['time'] === filemtime($reflection->getFileName()) && isset($cache['routes']) && is_string($cache['routes']);
         if ($validCache) {
             $routes = unserialize($cache['routes']);
         }
     }
     if (!$validCache) {
         $routes = $this->router->getRouteCollection()->all();
         $this->cacher->setFastCache($key, ['time' => filemtime($reflection->getFileName()), 'routes' => serialize($routes)]);
     }
     if (!isset($routes[$routeName])) {
         throw new \RuntimeException("Route with name `{$routeName}` does not exist");
     }
     $route = $routes[$routeName];
     $url = $this->router->generate($routeName, $this->pageStack->getRequest()->attributes->all());
     $page = Node::createPage($route->getOption('title'), parse_url($url)['path'], $route->getOption('theme'), $route->getOption('layout'));
     if ($route->getOption('meta')) {
         foreach ((array) $route->getOption('meta') as $key => $value) {
             $page->meta->set($key, $value);
         }
     }
     return $page;
 }
예제 #8
0
 /**
  * @return string
  */
 public function getBaseHref()
 {
     return $this->pageStack->getRequest()->getBasePath() . '/';
 }
예제 #9
0
 /**
  * Returns a permanent(301) redirectResponse object.
  *
  * @return RedirectResponse
  */
 public function redirectToStartPageAction()
 {
     $qs = $this->pageStack->getRequest()->getQueryString();
     $response = new RedirectResponse(($this->pageStack->getRequest()->getBaseUrl() ?: '') . ($qs ? '?' . $qs : ''), 301);
     return $response;
 }
예제 #10
0
 public function classWhenRoute($className, $routeName = null)
 {
     return $this->pageStack->getRequest()->attributes->get('_route') === $routeName ? $className : '';
 }
예제 #11
0
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
         //we need to reset all routes. They will anyway replaced by FrontendRouter::loadRoutes,
         //but to prevent caching conflicts, when a user removes a plugin for example
         //from a page, we need to know that without using actual caching.
         $this->routes = new RouteCollection();
         $this->urlMatcher->__construct($this->routes, $this->requestContext);
         //prepare for new master request: clear the PageResponse object
         $this->pageStack->setCurrentPage(null);
         $this->pageStack->setCurrentDomain(null);
         $this->pageStack->setPageResponse($this->pageResponseFactory->create());
         $this->frontendRouter->setRequest($request);
         $editorNodeId = (int) $this->pageStack->getRequest()->get('_jarves_editor_node');
         $editorDomainId = (int) $this->pageStack->getRequest()->get('_jarves_editor_domain');
         $domain = null;
         if ($editorDomainId) {
             $domain = $this->pageStack->getDomain($editorDomainId);
             if (!$domain) {
                 //we haven't found any domain that is responsible for this request
                 return;
             }
             $this->pageStack->setCurrentDomain($domain);
         }
         if ($editorNodeId) {
             //handle jarves content editor stuff
             //access is later checked
             if (!$editorNodeId && $domain) {
                 $editorNodeId = $domain->getStartnodeId();
             }
             $page = $this->pageStack->getPage($editorNodeId);
             if (!$page || !$page->isRenderable()) {
                 //we haven't found any page that is responsible for this request
                 return;
             }
             if (!$domain) {
                 $domain = $this->pageStack->getDomain($page->getDomainId());
             }
             $this->pageStack->setCurrentPage($page);
             $this->pageStack->setCurrentDomain($domain);
             $request->attributes->set('_controller', 'jarves.page_controller:handleAction');
         } else {
             //regular frontend route search
             //search domain
             if (!$domain) {
                 $domain = $this->frontendRouter->searchDomain();
                 if (!$domain) {
                     //we haven't found any domain that is responsible for this request
                     return;
                 }
                 $this->pageStack->setCurrentDomain($domain);
             }
             //search page
             $page = $this->frontendRouter->searchPage();
             if (!$page || !$page->isRenderable()) {
                 //we haven't found any page that is responsible for this request
                 return;
             }
             $this->pageStack->setCurrentPage($page);
             if ($response = $this->frontendRouter->loadRoutes($this->routes, $page)) {
                 //loadRoutes return in case of redirects and permissions a redirect or 404 response.
                 $event->setResponse($response);
                 return;
             }
             try {
                 //check routes in $this->route
                 parent::onKernelRequest($event);
             } catch (MethodNotAllowedException $e) {
             } catch (NotFoundHttpException $e) {
             }
         }
     }
 }