| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Superdesk Web Publisher Core Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2016 Sourcefabric z.ú. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2016 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace SWP\Bundle\CoreBundle\Enhancer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SWP\Component\TemplatesSystem\Gimme\Context\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use SWP\Component\TemplatesSystem\Gimme\Meta\Meta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use SWP\Bundle\CoreBundle\Resolver\TemplateNameResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SWP\Bundle\ContentBundle\Model\ArticleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\RouteObjectInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use SWP\Bundle\ContentBundle\Model\RouteInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use SWP\Bundle\CoreBundle\Controller\ContentController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class RouteEnhancer implements RouteEnhancerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var TemplateNameResolverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $templateNameResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var LoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $metaLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var Context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected $context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param TemplateNameResolverInterface $templateNameResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param LoaderInterface               $metaLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param Context                       $context | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 95 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         TemplateNameResolverInterface $templateNameResolver, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         LoaderInterface $metaLoader, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         Context $context | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 95 |  |         $this->templateNameResolver = $templateNameResolver; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 95 |  |         $this->metaLoader = $metaLoader; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 95 |  |         $this->context = $context; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 95 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Adjust route defaults and request attributes to our needs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param array   $defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 16 |  |     public function enhance(array $defaults, Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 16 |  |         $defaults['_controller'] = ContentController::class.'::renderPageAction'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 16 |  |         $defaults = $this->setArticleMeta($this->getContentFromDefaults($defaults), $request, $defaults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 14 |  |         $defaults = $this->setTemplateName($this->getContentFromDefaults($defaults), $defaults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 12 |  |         $defaults = $this->setRouteMeta($request, $defaults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 12 |  |         return $defaults; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Get article based on available parameters, set route type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param mixed   $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param array   $defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @throws NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 16 |  |     public function setArticleMeta($content, Request $request, array $defaults) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 16 |  |         $articleMeta = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 16 |  |         if (isset($defaults['slug'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 10 |  |             $articleMeta = $this->metaLoader->load('article', ['slug' => $defaults['slug']], LoaderInterface::SINGLE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 10 |  |             $defaults['type'] = RouteInterface::TYPE_COLLECTION; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 10 |  |             if (null === $articleMeta) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 10 |  |                 throw new NotFoundHttpException('Article was not found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } elseif ($content instanceof ArticleInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $articleMeta = $this->metaLoader->load('article', ['article' => $content], LoaderInterface::SINGLE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $defaults['type'] = RouteInterface::TYPE_CONTENT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if (null === $articleMeta) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 throw new NotFoundHttpException('Page was not found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 14 |  |         if ($articleMeta && $articleMeta->getValues() instanceof ArticleInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 8 |  |             $defaults[RouteObjectInterface::CONTENT_OBJECT] = $articleMeta->getValues(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 14 |  |         $request->attributes->set('articleMeta', $articleMeta); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 14 |  |         $defaults['_article_meta'] = $articleMeta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 14 |  |         return $defaults; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Resolve template name based on available data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param mixed $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param array $defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 14 |  |     public function setTemplateName($content, array $defaults) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 14 |  |         $route = isset($defaults[RouteObjectInterface::ROUTE_OBJECT]) ? $defaults[RouteObjectInterface::ROUTE_OBJECT] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 14 |  |         if ($content && null === $content->getRoute() && $route) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $defaults[RouteObjectInterface::TEMPLATE_NAME] = $this->templateNameResolver->resolve($route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 14 |  |         } elseif ($content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 8 |  |             $defaults[RouteObjectInterface::TEMPLATE_NAME] = $this->templateNameResolver->resolve($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 6 |  |             $defaults[RouteObjectInterface::TEMPLATE_NAME] = $this->templateNameResolver->resolve($route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 12 |  |         return $defaults; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param array   $defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 12 |  |     public function setRouteMeta(Request $request, array $defaults) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 12 |  |         $routeMeta = $this->metaLoader->load('route', ['route_object' => $defaults['_route_object']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 12 |  |         $request->attributes->set('routeMeta', $routeMeta); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 12 |  |         $defaults['_route_meta'] = $routeMeta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 12 |  |         if ($routeMeta instanceof Meta) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 12 |  |             $this->context->setCurrentPage($routeMeta); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 12 |  |         return $defaults; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @param array $defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @return ArticleInterface|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 16 |  |     private function getContentFromDefaults($defaults) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 16 |  |         if (isset($defaults[RouteObjectInterface::CONTENT_OBJECT])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 9 |  |             return $defaults[RouteObjectInterface::CONTENT_OBJECT]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 10 |  |         return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 170 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |  |