Completed
Push — master ( 943281...5c35c6 )
by Vladimir
02:38
created

PageManager::updateTwigVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
307
                $this->flatPages[$pageView->title] = $pageView->createJail();
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<allejo\stakx\Object\PageView>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
308
            }
309
        }
310 1
    }
311
312
    /**
313
     * Compile a given PageView
314
     *
315
     * @param string $filePath The file path to the PageView to compile
316
     * @param bool   $refresh  When set to true, the PageView will reread its contents
317
     *
318
     * @throws \Exception
319
     */
320 1
    private function compileFromFilePath ($filePath, $refresh = false)
321
    {
322 1
        if (!$this->isTracked($filePath))
323 1
        {
324
            throw new TrackedItemNotFoundException('PageView not found');
325
        }
326
327
        /** @var DynamicPageView|PageView|RepeaterPageView $pageView */
328 1
        $pageView = &$this->trackedItemsFlattened[$filePath];
329
330 1
        $this->compilePageView($pageView, $refresh);
331 1
    }
332
333
    /**
334
     * @param DynamicPageView|PageView|RepeaterPageView $pageView
335
     * @param bool                                      $refresh
336
     */
337 1
    private function compilePageView ($pageView, $refresh = false)
338
    {
339
        if ($refresh)
340 1
        {
341
            $pageView->refreshFileContent();
342
        }
343
344 1
        switch ($pageView->getType())
345
        {
346 1
            case PageView::REPEATER_TYPE:
347 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...
348 1
                $this->compileExpandedRedirects($pageView);
349 1
                break;
350
351
            case PageView::DYNAMIC_TYPE:
352
                $this->compileDynamicPageView($pageView);
353
                $this->compileNormalRedirects($pageView);
354
                break;
355
356
            case PageView::STATIC_TYPE:
357
                $this->compileStaticPageView($pageView);
358
                $this->compileNormalRedirects($pageView);
359
                break;
360 1
        }
361 1
    }
362
363
    /**
364
     * @param RepeaterPageView $pageView
365
     */
366 1
    private function compileRepeaterPageView (&$pageView)
367
    {
368 1
        $template = $this->createTemplate($pageView);
369 1
        $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...
370
371 1
        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...
372
        {
373 1
            $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...
374 1
            $pageView->setFrontMatter(array(
375 1
                'permalink' => $permalink->getEvaluated(),
376 1
                'iterators' => $permalink->getIterators()
377 1
            ));
378
379 1
            $output = $template->render(array(
380 1
                'this' => $pageView->createJail()
381 1
            ));
382
383 1
            $this->output->notice("Writing repeater file: {file}", array('file' => $pageView->getTargetFile()));
384 1
            $this->targetDir->writeFile($pageView->getTargetFile(), $output);
385 1
        }
386 1
    }
387
388
    /**
389
     * @param PageView $pageView
390
     */
391
    private function compileDynamicPageView (&$pageView)
392
    {
393
        $template = $this->createTemplate($pageView);
394
395
        $pageViewFrontMatter = $pageView->getFrontMatter(false);
396
        $collection = $pageViewFrontMatter['collection'];
397
398
        /** @var ContentItem $contentItem */
399
        foreach ($this->collections[$collection] as &$contentItem)
400
        {
401
            $output = $template->render(array(
402
                'this' => $contentItem->createJail()
403
            ));
404
405
            $this->output->notice("Writing file: {file}", array('file' => $contentItem->getTargetFile()));
406
            $this->targetDir->writeFile($contentItem->getTargetFile(), $output);
407
        }
408
    }
409
410
    /**
411
     * @param PageView $pageView
412
     */
413
    private function compileStaticPageView (&$pageView)
414
    {
415
        $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath());
416
417
        $template = $this->createTemplate($pageView);
418
        $output = $template->render(array(
419
            'this' => $pageView->createJail()
420
        ));
421
422
        $this->output->notice("Writing file: {file}", array('file' => $pageView->getTargetFile()));
423
        $this->targetDir->writeFile($pageView->getTargetFile(), $output);
424
    }
425
426
    /**
427
     * @param DynamicPageView|PageView $pageView
428
     */
429
    private function compileNormalRedirects (&$pageView)
430
    {
431
        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...
432
        {
433
            $redirectPageView = PageView::createRedirect(
434
                $redirect,
435
                $pageView->getPermalink(),
436
                $this->redirectTemplate
437
            );
438
439
            $this->compilePageView($redirectPageView);
440
        }
441
    }
442
443
    /**
444
     * @param RepeaterPageView $pageView
445
     */
446 1
    private function compileExpandedRedirects (&$pageView)
447
    {
448 1
        $permalinks = $pageView->getRepeaterPermalinks();
449
450
        /** @var ExpandedValue[] $repeaterRedirect */
451 1
        foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect)
452
        {
453
            /**
454
             * @var int           $index
455
             * @var ExpandedValue $redirect
456
             */
457
            foreach ($repeaterRedirect as $index => $redirect)
458
            {
459
                $redirectPageView = PageView::createRedirect(
460
                    $redirect->getEvaluated(),
461
                    $permalinks[$index]->getEvaluated(),
462
                    $this->redirectTemplate
463
                );
464
465
                $this->compilePageView($redirectPageView);
466
            }
467 1
        }
468 1
    }
469
470
    /**
471
     * Add a static PageView to the menu array. Dynamic PageViews are not added to the menu
472
     *
473
     * @param PageView $pageView
474
     */
475 1
    private function addToSiteMenu (&$pageView)
476
    {
477
        $frontMatter = $pageView->getFrontMatter();
478
479
        if (!array_key_exists('permalink', $frontMatter) ||
480
            (array_key_exists('menu', $frontMatter) && !$frontMatter['menu']))
481
        {
482
            return;
483
        }
484
485
        $url = $pageView->getPermalink();
486 1
        $root = &$this->siteMenu;
487 1
        $permalink = trim($url, DIRECTORY_SEPARATOR);
488
        $dirs = explode(DIRECTORY_SEPARATOR, $permalink);
489
490
        while (count($dirs) > 0)
491
        {
492
            $name = array_shift($dirs);
493
            $name = (!empty($name)) ? $name : '.';
494
495
            if (!is_null($name) && count($dirs) == 0)
496
            {
497
                $children = array();
498
499
                if (array_key_exists($name, $root) && is_array($root[$name]))
500
                {
501
                    $children = $root[$name]['children'];
502
                }
503
504
                $root[$name] = &$pageView;
505
                $root = &$root[$name]->getChildren();
506
507
                if (!empty($children))
508
                {
509
                    $root = $children;
510
                }
511
            }
512
            else
513
            {
514
                $root[$name]['children'] = array();
515
                $root = &$root[$name]['children'];
516
            }
517
        }
518
    }
519
520
    /**
521
     * @param PageView $pageView
522
     *
523
     * @return Twig_Template
524
     * @throws Twig_Error_Syntax
525
     */
526 1
    private function createTemplate (&$pageView)
527
    {
528
        try
529
        {
530 1
            $template = $this->twig->createTemplate($pageView->getContent());
531
532 1
            $this->trackParentTwigTemplate($template, $pageView);
533
534 1
            return $template;
535
        }
536
        catch (Twig_Error_Syntax $e)
537
        {
538
            $e->setTemplateLine($e->getTemplateLine() + $pageView->getLineOffset());
539
            $e->setTemplateName($pageView->getRelativeFilePath());
540
541
            throw $e;
542
        }
543
    }
544
545
    /**
546
     * Find the parent Twig templates of the given template and keep a list of it
547
     *
548
     * @param Twig_Template $template The template created from the PageView's content
549
     * @param PageView      $pageView The PageView that has this content. Used to keep a reference of PageViews
550
     */
551 1
    private function trackParentTwigTemplate ($template, &$pageView)
552
    {
553 1
        if (!$this->tracking) { return; }
554
555
        /** @var Twig_Template $parent */
556
        $parent = $template->getParent(array());
557
558
        while ($parent !== false)
559
        {
560
            $filePath = $this->fs->getRelativePath($parent->getSourceContext()->getPath());
561
562
            $this->twigExtendsDeps[$filePath][(string)$pageView->getFilePath()] = &$pageView;
563
            $parent = $parent->getParent(array());
564
        }
565
    }
566
}