File size: 2,808 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Cache;

use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

class MiddlewareCacheWarmer implements CacheWarmerInterface
{
    private ?string $cacheFile = null;

    /**
     * @var \SplPriorityQueue|\ReflectionClass[]
     */
    private \SplPriorityQueue $specs;

    public function __construct(
        private string $env
    ) {
        $this->specs     = new \SplPriorityQueue();
    }

    /**
     * @inerhitDoc
     */
    public function warmUp(string $cacheDirectory)
    {
        $this->cacheFile = sprintf('%s/middlewares.cache.php', $cacheDirectory);
        $this->createCacheFile($cacheDirectory);

        return [];
    }

    public function isOptional(): bool
    {
        return false;
    }

    private function createCacheFile($cacheDirectory): void
    {
        $middlewarsDir = __DIR__.'/../../../middlewares';

        $this->loadFromDirectory($middlewarsDir);

        $envDir = $middlewarsDir.'/'.ucfirst($this->env);

        if (file_exists($envDir)) {
            $this->loadFromDirectory($envDir, $this->env);
        }

        if (file_exists($this->cacheFile)) {
            unlink($this->cacheFile);
        }

        if (false === file_exists($cacheDirectory)) {
            mkdir($cacheDirectory, 0777, true);
        }

        $data  = [];
        $this->specs->setExtractFlags(\SplPriorityQueue::EXTR_DATA);

        /** @var \ReflectionClass $middleware */
        foreach ($this->specs as $middleware) {
            $data[] = $middleware->getName();
        }

        $content = sprintf('<?php return %s;', var_export($data, true));

        file_put_contents($this->cacheFile, $content);
    }

    private function loadFromDirectory(string $directory, ?string $env = null): void
    {
        $glob = glob($directory.'/*Middleware.php');

        if (!empty($glob)) {
            $this->addMiddlewares($glob, $env);
        }
    }

    private function addMiddlewares(array $middlewares, ?string $env = null): void
    {
        $prefix = 'Mautic\\Middleware\\';

        if ($env) {
            $prefix .= ucfirst($env).'\\';
        }

        foreach ($middlewares as $middleware) {
            $this->push($prefix.basename(substr($middleware, 0, -4)));
        }
    }

    private function push(string $middlewareClass): void
    {
        try {
            $reflection = new \ReflectionClass($middlewareClass);
            $priority   = $reflection->getConstant('PRIORITY');

            $this->specs->insert($reflection, $priority);
        } catch (\ReflectionException) {
            /* If there's an error getting the kernel class, it's
             * an invalid middleware. If it's invalid, don't push
             * it to the stack
             */
        }
    }
}