Completed
Push — master ( a23ebb...50fad5 )
by Vladimir
06:49
created

PageManager::updatePageView()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 6
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use allejo\stakx\Exception\FileAwareException;
6
use allejo\stakx\Exception\TrackedItemNotFoundException;
7
use allejo\stakx\FrontMatter\ExpandedValue;
8
use allejo\stakx\Object\ContentItem;
9
use allejo\stakx\Object\DynamicPageView;
10
use allejo\stakx\Object\JailObject;
11
use allejo\stakx\Object\PageView;
12
use allejo\stakx\Object\RepeaterPageView;
13
use allejo\stakx\System\FileExplorer;
14
use allejo\stakx\System\Folder;
15
use Twig_Error_Syntax;
16
use Twig_Template;
17
18
/**
19
 * This class is responsible for handling all of the PageViews within a website.
20
 *
21
 * PageManager will parse all available dynamic and static PageViews. After, dynamic PageViews will be prepared by
22
 * setting the appropriate values for each ContentItem such as permalinks. Lastly, this class will compile all of the
23
 * PageViews and write them to the target directory.
24
 *
25
 * @package allejo\stakx\Manager
26
 */
27
class PageManager extends TrackingManager
28
{
29
    /**
30
     * The relative (to the stakx project) file path to the redirect template
31
     *
32
     * @var string|bool
33
     */
34
    private $redirectTemplate;
35
36
    /**
37
     * @var PageView[]
38
     */
39
    private $twigExtendsDeps;
40
41
    /**
42
     * @var ContentItem[][]
43
     */
44
    private $collections;
45
46
    /**
47
     * @var Folder
48
     */
49
    private $targetDir;
50
51
    /**
52
     * @var PageView[]
53
     */
54
    private $flatPages;
55
56
    /**
57
     * @var PageView[]
58
     */
59
    private $siteMenu;
60
61
    /**
62
     * @var array
63
     */
64
    private $twigOpts;
65
66 1
    /**
67 1
     * @var \Twig_Environment
68 1
     */
69
    private $twig;
70 1
71 1
    /**
72
     * PageManager constructor
73 1
     */
74
    public function __construct()
75
    {
76
        parent::__construct();
77
78 1
        $this->redirectTemplate = false;
79
        $this->twigExtendsDeps = array();
80
        $this->collections = array();
81
        $this->flatPages = array();
82
        $this->siteMenu = array();
83
    }
84
85
    /**
86
     * Give this manager the collections we'll be using for dynamic PageViews
87
     *
88 1
     * @param ContentItem[][] $collections
89
     */
90 1
    public function setCollections (&$collections)
91 1
    {
92
        $this->collections = &$collections;
93 1
    }
94
95 1
    /**
96 1
     * Set the template used for redirects
97
     *
98 1
     * @param false|string $filePath The path to the redirect template
99 1
     */
100
    public function setRedirectTemplate ($filePath)
101 1
    {
102
        $this->redirectTemplate = $filePath;
103 1
    }
104 1
105 1
    /**
106 1
     * The location where the compiled website will be written to
107 1
     *
108
     * @param Folder $folder The relative target directory as specified from the configuration file
109 1
     */
110 1
    public function setTargetFolder (&$folder)
111
    {
112
        $this->targetDir = &$folder;
113
    }
114
115
    public function configureTwig ($configuration, $options)
116
    {
117
        $this->twigOpts['configuration'] = $configuration;
118
        $this->twigOpts['options']       = $options;
119
120
        $this->createTwigManager();
121
    }
122
123
    public function getFlatPages ()
124
    {
125
        return $this->flatPages;
126
    }
127
128
    /**
129
     * An array representing the website's menu structure with children and grandchildren made from static PageViews
130
     *
131
     * @return JailObject[]
132
     */
133
    public function getSiteMenu ()
134
    {
135
        $jailedMenu = array();
136
137
        foreach ($this->siteMenu as $key => $value)
138
        {
139
            // If it's an array, it means the parent is hidden from the site menu therefore its children should be too
140
            if (is_array($this->siteMenu[$key]))
141
            {
142
                continue;
143
            }
144
145
            $jailedMenu[$key] = $value->createJail();
146
        }
147
148
        return $jailedMenu;
149
    }
150 1
151
    /**
152 1
     * Go through all of the PageView directories and create a respective PageView for each and classify them as a
153
     * dynamic or static PageView.
154
     *
155
     * @param $pageViewFolders
156
     */
157
    public function parsePageViews ($pageViewFolders)
158
    {
159 1
        if (empty($pageViewFolders)) { return; }
160
161 1
        /**
162
         * The name of the folder where PageViews are located
163 1
         *
164 1
         * @var $pageViewFolder string
165
         */
166
        foreach ($pageViewFolders as $pageViewFolderName)
167
        {
168
            $pageViewFolder = $this->fs->absolutePath($pageViewFolderName);
169 1
170 1
            if (!$this->fs->exists($pageViewFolder))
171
            {
172 1
                continue;
173 1
            }
174 1
175 1
            // @TODO Replace this with a regular expression or have wildcard support
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
176
            $this->scanTrackableItems($pageViewFolder, array(
177
                'refresh' => false,
178
                'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES
179
            ), array('.html', '.twig'));
180 1
            $this->saveFolderDefinition($pageViewFolderName);
181
        }
182 1
    }
183
184 1
    /**
185 1
     * Compile dynamic and static PageViews
186 1
     */
187
    public function compileAll ()
188
    {
189
        foreach (array_keys($this->trackedItemsFlattened) as $filePath)
190
        {
191
            $this->compileFromFilePath($filePath);
192
        }
193
    }
194
195
    public function compileSome ($filter = array())
196
    {
197
        /** @var PageView $pageView */
198
        foreach ($this->trackedItemsFlattened as $pageView)
199
        {
200
            if ($pageView->hasTwigDependency($filter['namespace'], $filter['dependency']))
201
            {
202
                $this->compilePageView($pageView);
203
            }
204
        }
205
    }
206
207
    /**
208
     * @param ContentItem $contentItem
209
     */
210
    public function compileContentItem (&$contentItem)
211
    {
212
        $pageView = $contentItem->getPageView();
213
214
        // This ContentItem doesn't have an individual PageView dedicated to displaying this item
215
        if (is_null($pageView))
216
        {
217
            return;
218
        }
219
220
        $template = $this->createTemplate($pageView);
221
        $contentItem->evaluateFrontMatter(
222
            $pageView->getFrontMatter(false)
223
        );
224
225
        $output = $template->render(array(
226
            'this' => $contentItem
227
        ));
228
229
        $this->targetDir->writeFile($contentItem->getTargetFile(), $output);
230
    }
231
232
    /**
233
     * Add a new ContentItem to the respective parent PageView of the ContentItem
234
     *
235
     * @param ContentItem $contentItem
236
     */
237
    public function updatePageView ($contentItem)
238
    {
239
        /** @var DynamicPageView $pageView */
240
        foreach ($this->trackedItems['dynamic'] as &$pageView)
241
        {
242
            $fm = $pageView->getFrontMatter(false);
243
244
            if ($fm['collection'] == $contentItem->getCollection())
245
            {
246
                $pageView->addContentItem($contentItem);
247
            }
248
        }
249
    }
250
251
    /**
252
     * Update an existing Twig variable that's injected globally
253
     *
254
     * @param string $variable
255
     * @param string $value
256
     */
257
    public function updateTwigVariable ($variable, $value)
258 1
    {
259
        $this->twig->addGlobal($variable, $value);
260 1
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function isTracked($filePath)
266
    {
267
        return (parent::isTracked($filePath) || isset($this->twigExtendsDeps[$filePath]));
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273
    public function refreshItem($filePath)
274
    {
275
        if (parent::isTracked($filePath))
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (isTracked() instead of refreshItem()). Are you sure this is correct? If so, you might want to change this to $this->isTracked().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
276
        {
277
            $this->compileFromFilePath($filePath, true);
278
279
            return;
280
        }
281
282
        $this->createTwigManager();
283
284
        foreach ($this->twigExtendsDeps[$filePath] as $pageView)
0 ignored issues
show
Bug introduced by
The expression $this->twigExtendsDeps[$filePath] of type object<allejo\stakx\Object\PageView> is not traversable.
Loading history...
285
        {
286 1
            $this->compilePageView($pageView);
287
        }
288 1
    }
289 1
290
    /**
291 1
     * {@inheritdoc}
292 1
     */
293
    protected function handleTrackableItem($filePath, $options = array())
294
    {
295
        $pageView  = PageView::create($filePath);
296
        $namespace = $pageView->getType();
297
298
        if ($namespace == PageView::DYNAMIC_TYPE)
299
        {
300
            $frontMatter = $pageView->getFrontMatter(false);
301
            $collection = $frontMatter['collection'];
302
303 1
            foreach ($this->collections[$collection] as &$item)
304 1
            {
305
                $item->evaluateFrontMatter($frontMatter);
306 1
                $pageView->addContentItem($item);
307
            }
308 1
        }
309 1
310 1
        $this->addObjectToTracker($pageView, $pageView->getRelativeFilePath(), $namespace);
311
        $this->saveTrackerOptions($pageView->getRelativeFilePath(), array(
312 1
            'viewType' => $namespace
313 1
        ));
314 1
315 1
        if ($namespace == PageView::STATIC_TYPE && !empty($pageView['title']))
316 1
        {
317
            $this->addToSiteMenu($pageView);
318
            $this->flatPages[$pageView['title']] = $pageView->createJail();
319
        }
320
    }
321
322
    /**
323
     * Create a Twig environment
324
     */
325
    private function createTwigManager ()
326 1
    {
327
        $twig = new TwigManager();
328 1
        $twig->configureTwig(
329 1
            $this->twigOpts['configuration'],
330
            $this->twigOpts['options']
331
        );
332
333
        $this->twig = TwigManager::getInstance();
334 1
    }
335
336 1
    /**
337 1
     * Compile a given PageView
338
     *
339
     * @param string $filePath The file path to the PageView to compile
340
     * @param bool   $refresh  When set to true, the PageView will reread its contents
341
     *
342
     * @throws \Exception
343 1
     */
344
    private function compileFromFilePath ($filePath, $refresh = false)
345
    {
346 1
        if (!$this->isTracked($filePath))
347
        {
348
            throw new TrackedItemNotFoundException('PageView not found');
349
        }
350 1
351
        /** @var DynamicPageView|PageView|RepeaterPageView $pageView */
352 1
        $pageView = &$this->trackedItemsFlattened[$filePath];
353 1
354 1
        try
355 1
        {
356
            $this->compilePageView($pageView, $refresh);
357 1
        }
358
        catch (\Exception $e)
359
        {
360
            $exception = new FileAwareException(
361
                $e->getMessage(),
362 1
                $e->getCode(),
363 1
                $e,
364 1
                $filePath
365 1
            );
366 1
367 1
            throw $exception;
368
        }
369
    }
370
371
    /**
372 1
     * @param DynamicPageView|PageView|RepeaterPageView $pageView
373
     * @param bool                                      $refresh
374 1
     */
375 1
    private function compilePageView ($pageView, $refresh = false)
376
    {
377 1
        if ($refresh)
378
        {
379 1
            $pageView->refreshFileContent();
380 1
        }
381 1
382 1
        switch ($pageView->getType())
383 1
        {
384
            case PageView::REPEATER_TYPE:
385 1
                $this->compileRepeaterPageView($pageView);
0 ignored issues
show
Compatibility introduced by
$pageView of type object<allejo\stakx\Object\PageView> is not a sub-type of object<allejo\stakx\Object\RepeaterPageView>. It seems like you assume a child class of the class allejo\stakx\Object\PageView to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
386 1
                $this->compileExpandedRedirects($pageView);
387 1
                break;
388
389 1
            case PageView::DYNAMIC_TYPE:
390 1
                $this->compileDynamicPageView($pageView);
391 1
                $this->compileNormalRedirects($pageView);
392 1
                break;
393
394
            case PageView::STATIC_TYPE:
395
                $this->compileStaticPageView($pageView);
396
                $this->compileNormalRedirects($pageView);
397
                break;
398
        }
399
    }
400
401
    /**
402
     * @param RepeaterPageView $pageView
403
     */
404
    private function compileRepeaterPageView (&$pageView)
405
    {
406
        $template = $this->createTemplate($pageView);
407
        $pageView->rewindPermalink();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class allejo\stakx\Object\PageView as the method rewindPermalink() does only exist in the following sub-classes of allejo\stakx\Object\PageView: allejo\stakx\Object\RepeaterPageView. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
408
409
        foreach ($pageView->getRepeaterPermalinks() as $permalink)
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class allejo\stakx\Object\PageView as the method getRepeaterPermalinks() does only exist in the following sub-classes of allejo\stakx\Object\PageView: allejo\stakx\Object\RepeaterPageView. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
410
        {
411
            $pageView->bumpPermalink();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class allejo\stakx\Object\PageView as the method bumpPermalink() does only exist in the following sub-classes of allejo\stakx\Object\PageView: allejo\stakx\Object\RepeaterPageView. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
412
            $pageView->setFrontMatter(array(
413
                'permalink' => $permalink->getEvaluated(),
414
                'iterators' => $permalink->getIterators()
415
            ));
416
417
            $output = $template->render(array(
418
                'this' => $pageView->createJail()
419 1
            ));
420
421 1
            $this->output->notice("Writing repeater file: {file}", array('file' => $pageView->getTargetFile()));
422
            $this->targetDir->writeFile($pageView->getTargetFile(), $output);
423 1
        }
424 1
    }
425 1
426 1
    /**
427
     * @param PageView $pageView
428 1
     */
429 1
    private function compileDynamicPageView (&$pageView)
430 1
    {
431
        $template = $this->createTemplate($pageView);
432
433
        $pageViewFrontMatter = $pageView->getFrontMatter(false);
434
        $collection = $pageViewFrontMatter['collection'];
435 1
436
        /** @var ContentItem $contentItem */
437 1
        foreach ($this->collections[$collection] as &$contentItem)
438
        {
439
            $output = $template->render(array(
440
                'this' => $contentItem->createJail()
441
            ));
442
443
            $this->output->notice("Writing file: {file}", array('file' => $contentItem->getTargetFile()));
444
            $this->targetDir->writeFile($contentItem->getTargetFile(), $output);
445
        }
446 1
    }
447 1
448
    /**
449
     * @param PageView $pageView
450
     */
451
    private function compileStaticPageView (&$pageView)
452 1
    {
453
        $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath());
454 1
455
        $template = $this->createTemplate($pageView);
456
        $output = $template->render(array(
457 1
            'this' => $pageView->createJail()
458
        ));
459
460
        $this->output->notice("Writing file: {file}", array('file' => $pageView->getTargetFile()));
461
        $this->targetDir->writeFile($pageView->getTargetFile(), $output);
462
    }
463
464
    /**
465
     * @param DynamicPageView|PageView $pageView
466
     */
467
    private function compileNormalRedirects (&$pageView)
468
    {
469
        foreach ($pageView->getRedirects() as $redirect)
0 ignored issues
show
Bug introduced by
The expression $pageView->getRedirects() of type null|array<integer,string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
470
        {
471
            $redirectPageView = PageView::createRedirect(
472
                $redirect,
473 1
                $pageView->getPermalink(),
474 1
                $this->redirectTemplate
475
            );
476
477
            $this->compilePageView($redirectPageView);
478
        }
479
    }
480
481 1
    /**
482
     * @param RepeaterPageView $pageView
483 1
     */
484
    private function compileExpandedRedirects (&$pageView)
485 1
    {
486 1
        $permalinks = $pageView->getRepeaterPermalinks();
487
488
        /** @var ExpandedValue[] $repeaterRedirect */
489
        foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect)
490 1
        {
491
            /**
492 1
             * @var int           $index
493 1
             * @var ExpandedValue $redirect
494 1
             */
495 1
            foreach ($repeaterRedirect as $index => $redirect)
496
            {
497 1
                $redirectPageView = PageView::createRedirect(
498 1
                    $redirect->getEvaluated(),
499
                    $permalinks[$index]->getEvaluated(),
500 1
                    $this->redirectTemplate
501
                );
502 1
503 1
                $this->compilePageView($redirectPageView);
504
            }
505 1
        }
506 1
    }
507 1
508 1
    /**
509
     * Add a static PageView to the menu array. Dynamic PageViews are not added to the menu
510
     *
511
     * @param PageView $pageView
512
     */
513 1
    private function addToSiteMenu (&$pageView)
514 1
    {
515
        $frontMatter = $pageView->getFrontMatter();
516
517 1
        if (isset($frontMatter['menu']) && !$frontMatter['menu'])
518 1
        {
519
            return;
520 1
        }
521 1
522
        $url = trim($pageView->getPermalink(), '/');
523
524
        if (empty($url))
525
        {
526
            return;
527
        }
528
529 1
        $root = &$this->siteMenu;
530
        $dirs = explode('/', $url);
531
532
        while (count($dirs) > 0)
533 1
        {
534
            $name = array_shift($dirs);
535 1
            $name = (!empty($name)) ? $name : '.';
536
537 1
            if (!is_null($name) && count($dirs) == 0)
538
            {
539
                if (isset($root[$name]) && is_array($root[$name]))
540
                {
541
                    $children = &$pageView->getChildren();
542
                    $children = $root[$name]['children'];
543
                }
544
545
                $root[$name] = &$pageView;
546
            }
547
            else
548
            {
549
                if (!isset($root[$name]['children']))
550
                {
551
                    $root[$name]['children'] = array();
552
                }
553
554 1
                $root = &$root[$name]['children'];
555
            }
556 1
        }
557
    }
558
559
    /**
560
     * @param PageView $pageView
561
     *
562
     * @return Twig_Template
563
     * @throws Twig_Error_Syntax
564
     */
565
    private function createTemplate (&$pageView)
566
    {
567
        try
568
        {
569
            $template = $this->twig->createTemplate($pageView->getContent());
570
571
            $this->trackParentTwigTemplate($template, $pageView);
572
573
            return $template;
574
        }
575
        catch (Twig_Error_Syntax $e)
576
        {
577
            $e->setTemplateLine($e->getTemplateLine() + $pageView->getLineOffset());
578
            $e->setTemplateName($pageView->getRelativeFilePath());
0 ignored issues
show
Deprecated Code introduced by
The method Twig_Error::setTemplateName() has been deprecated with message: since 1.29 (to be removed in 2.0). Use setSourceContext() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
579
580
            throw $e;
581
        }
582
    }
583
584
    /**
585
     * Find the parent Twig templates of the given template and keep a list of it
586
     *
587
     * @param Twig_Template $template The template created from the PageView's content
588
     * @param PageView      $pageView The PageView that has this content. Used to keep a reference of PageViews
589
     */
590
    private function trackParentTwigTemplate ($template, &$pageView)
591
    {
592
        if (!$this->tracking) { return; }
593
594
        /** @var Twig_Template $parent */
595
        $parent = $template->getParent(array());
596
597
        while ($parent !== false)
598
        {
599
            $filePath = $this->fs->getRelativePath($parent->getSourceContext()->getPath());
600
601
            $this->twigExtendsDeps[$filePath][(string)$pageView->getFilePath()] = &$pageView;
602
            $parent = $parent->getParent(array());
603
        }
604
    }
605
}