Completed
Pull Request — master (#69)
by Vladimir
02:44
created

AssetEngineSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\EventSubscriber;
9
10
use allejo\stakx\AssetEngine\AssetEngine;
11
use allejo\stakx\AssetEngine\AssetEngineManager;
12
use allejo\stakx\Document\StaticPageView;
13
use allejo\stakx\Event\PageManagerPostProcess;
14
use allejo\stakx\Filesystem\FileExplorer;
15
use allejo\stakx\Filesystem\FilesystemLoader as fs;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
18
class AssetEngineSubscriber implements EventSubscriberInterface
19
{
20
    private $assetEngineManager;
21
22
    public function __construct(AssetEngineManager $assetEngineManager)
23
    {
24
        $this->assetEngineManager = $assetEngineManager;
25
    }
26
27
    public function processAssetEnginePageView(PageManagerPostProcess $event)
28
    {
29
        /**
30
         * @var string      $folder
31
         * @var AssetEngine $engine
32
         */
33
        foreach ($this->assetEngineManager->getFoldersToWatch() as $folder => $engine)
34
        {
35
            $assetFolder = fs::absolutePath($folder);
0 ignored issues
show
Documentation introduced by
$folder is of type integer|string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
37
            if (!fs::exists($assetFolder))
38
            {
39
                continue;
40
            }
41
42
            $extensions = [];
43
44
            foreach ($engine->getExtensions() as $extension)
45
            {
46
                $extensions[] = "/.{$extension}.twig$/";
47
            }
48
49
            $explorer = FileExplorer::create($assetFolder, [], $extensions, FileExplorer::IGNORE_DIRECTORIES);
50
51
            foreach ($explorer as $file)
52
            {
53
                $assetPageView = new StaticPageView($file);
54
                $compiled = $engine->parse($assetPageView->getContent());
55
                $assetPageView->setContent($compiled);
56
57
                $event->getPageManager()->trackNewPageView($assetPageView);
58
            }
59
        }
60
    }
61
62
    public static function getSubscribedEvents()
63
    {
64
        return [
65
            PageManagerPostProcess::NAME => 'processAssetEnginePageView',
66
        ];
67
    }
68
}
69