"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.11.5/core-bundle/src/Controller/AbstractFragmentController.php" (23 Jun 2021, 4427 Bytes) of package /linux/www/contao-4.11.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractFragmentController.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 4.11.2_vs_4.11.3.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 /*
    6  * This file is part of Contao.
    7  *
    8  * (c) Leo Feyer
    9  *
   10  * @license LGPL-3.0-or-later
   11  */
   12 
   13 namespace Contao\CoreBundle\Controller;
   14 
   15 use Contao\CoreBundle\Fragment\FragmentOptionsAwareInterface;
   16 use Contao\CoreBundle\Routing\ScopeMatcher;
   17 use Contao\FrontendTemplate;
   18 use Contao\Model;
   19 use Contao\PageModel;
   20 use Contao\StringUtil;
   21 use Contao\Template;
   22 use Symfony\Component\DependencyInjection\Container;
   23 use Symfony\Component\HttpFoundation\RequestStack;
   24 
   25 abstract class AbstractFragmentController extends AbstractController implements FragmentOptionsAwareInterface
   26 {
   27     /**
   28      * @var array
   29      */
   30     protected $options = [];
   31 
   32     public function setFragmentOptions(array $options): void
   33     {
   34         $this->options = $options;
   35     }
   36 
   37     /**
   38      * @return array<string>
   39      */
   40     public static function getSubscribedServices()
   41     {
   42         $services = parent::getSubscribedServices();
   43 
   44         $services['request_stack'] = RequestStack::class;
   45         $services['contao.routing.scope_matcher'] = ScopeMatcher::class;
   46 
   47         return $services;
   48     }
   49 
   50     protected function getPageModel(): ?PageModel
   51     {
   52         $request = $this->get('request_stack')->getCurrentRequest();
   53 
   54         if (null === $request || !$request->attributes->has('pageModel')) {
   55             return null;
   56         }
   57 
   58         $pageModel = $request->attributes->get('pageModel');
   59 
   60         if ($pageModel instanceof PageModel) {
   61             return $pageModel;
   62         }
   63 
   64         if (
   65             isset($GLOBALS['objPage'])
   66             && $GLOBALS['objPage'] instanceof PageModel
   67             && (int) $GLOBALS['objPage']->id === (int) $pageModel
   68         ) {
   69             return $GLOBALS['objPage'];
   70         }
   71 
   72         $this->initializeContaoFramework();
   73 
   74         /** @var PageModel $pageAdapter */
   75         $pageAdapter = $this->get('contao.framework')->getAdapter(PageModel::class);
   76 
   77         return $pageAdapter->findByPk((int) $pageModel);
   78     }
   79 
   80     /**
   81      * Creates a template by name or from the "customTpl" field of the model.
   82      */
   83     protected function createTemplate(Model $model, string $templateName): Template
   84     {
   85         if (isset($this->options['template'])) {
   86             $templateName = $this->options['template'];
   87         }
   88 
   89         if ($model->customTpl) {
   90             $request = $this->get('request_stack')->getCurrentRequest();
   91 
   92             // Use the custom template unless it is a back end request
   93             if (null === $request || !$this->get('contao.routing.scope_matcher')->isBackendRequest($request)) {
   94                 $templateName = $model->customTpl;
   95             }
   96         }
   97 
   98         $template = $this->get('contao.framework')->createInstance(FrontendTemplate::class, [$templateName]);
   99         $template->setData($model->row());
  100 
  101         return $template;
  102     }
  103 
  104     /**
  105      * @param string|array $headline
  106      */
  107     protected function addHeadlineToTemplate(Template $template, $headline): void
  108     {
  109         $data = StringUtil::deserialize($headline);
  110         $template->headline = \is_array($data) ? $data['value'] : $data;
  111         $template->hl = \is_array($data) ? $data['unit'] : 'h1';
  112     }
  113 
  114     /**
  115      * @param string|array $cssID
  116      */
  117     protected function addCssAttributesToTemplate(Template $template, string $templateName, $cssID, array $classes = null): void
  118     {
  119         $data = StringUtil::deserialize($cssID, true);
  120         $template->class = trim($templateName.' '.($data[1] ?? ''));
  121         $template->cssID = !empty($data[0]) ? ' id="'.$data[0].'"' : '';
  122 
  123         if (!empty($classes)) {
  124             $template->class .= ' '.implode(' ', $classes);
  125         }
  126     }
  127 
  128     protected function addPropertiesToTemplate(Template $template, array $properties): void
  129     {
  130         foreach ($properties as $k => $v) {
  131             $template->{$k} = $v;
  132         }
  133     }
  134 
  135     protected function addSectionToTemplate(Template $template, string $section): void
  136     {
  137         $template->inColumn = $section;
  138     }
  139 
  140     /**
  141      * Returns the type from the class name.
  142      */
  143     protected function getType(): string
  144     {
  145         if (isset($this->options['type'])) {
  146             return $this->options['type'];
  147         }
  148 
  149         $className = ltrim(strrchr(static::class, '\\'), '\\');
  150 
  151         if ('Controller' === substr($className, -10)) {
  152             $className = substr($className, 0, -10);
  153         }
  154 
  155         return Container::underscore($className);
  156     }
  157 }