summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Application/Libraries/Library.php
blob: 63e50b2a52764eb9f67ffe9d4e1a48987ddd2dcb (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/* Icinga Web 2 | (c) 2020 Icinga GmbH | GPLv2+ */

namespace Icinga\Application\Libraries;

use CallbackFilterIterator;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\Json\JsonDecodeException;
use Icinga\Util\Json;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class Library
{
    /** @var string */
    protected $path;

    /** @var string */
    protected $jsAssetPath;

    /** @var string */
    protected $cssAssetPath;

    /** @var string */
    protected $staticAssetPath;

    /** @var string */
    protected $version;

    /** @var array */
    protected $metaData;

    /** @var array */
    protected $assets;

    /**
     * Create a new Library
     *
     * @param string $path
     */
    public function __construct($path)
    {
        $this->path = $path;
    }

    /**
     * Get this library's path
     *
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * Get path of this library's JS assets
     *
     * @return string
     */
    public function getJsAssetPath()
    {
        $this->assets();
        return $this->jsAssetPath;
    }

    /**
     * Get path of this library's CSS assets
     *
     * @return string
     */
    public function getCssAssetPath()
    {
        $this->assets();
        return $this->cssAssetPath;
    }

    /**
     * Get path of this library's static assets
     *
     * @return string
     */
    public function getStaticAssetPath()
    {
        $this->assets();
        return $this->staticAssetPath;
    }

    /**
     * Get this library's name
     *
     * @return string
     */
    public function getName()
    {
        return $this->metaData()['name'];
    }

    /**
     * Get this library's version
     *
     * @return string
     */
    public function getVersion()
    {
        if ($this->version === null) {
            if (isset($this->metaData()['version'])) {
                $this->version = trim(ltrim($this->metaData()['version'], 'v'));
            } else {
                $versionFile = $this->path . DIRECTORY_SEPARATOR . 'VERSION';
                if (file_exists($versionFile)) {
                    $this->version = trim(ltrim(file_get_contents($versionFile), 'v'));
                } else {
                    $this->version = '';
                }
            }
        }

        return $this->version;
    }

    /**
     * Check whether the given package is required
     *
     * @param string $vendor The vendor of the project
     * @param string $project The project's name
     *
     * @return bool
     */
    public function isRequired($vendor, $project)
    {
        // Ensure the parts are lowercase and separated by dashes, not capital letters
        $project = strtolower(join('-', preg_split('/\w(?=[A-Z])/', $project)));

        return isset($this->metaData()['require'][strtolower($vendor) . '/' . $project]);
    }

    /**
     * Get this library's JS assets
     *
     * @return string[] Asset paths
     */
    public function getJsAssets()
    {
        return $this->assets()['js'];
    }

    /**
     * Get this library's CSS assets
     *
     * @return string[] Asset paths
     */
    public function getCssAssets()
    {
        return $this->assets()['css'];
    }

    /**
     * Get this library's static assets
     *
     * @return string[] Asset paths
     */
    public function getStaticAssets()
    {
        return $this->assets()['static'];
    }

    /**
     * Register this library's autoloader
     *
     * @return void
     */
    public function registerAutoloader()
    {
        $autoloaderPath = join(DIRECTORY_SEPARATOR, [$this->path, 'vendor', 'autoload.php']);
        if (file_exists($autoloaderPath)) {
            require_once $autoloaderPath;
        }
    }

    /**
     * Parse and return this library's metadata
     *
     * @return array
     *
     * @throws ConfigurationError
     * @throws JsonDecodeException
     */
    protected function metaData()
    {
        if ($this->metaData === null) {
            $metaData = @file_get_contents($this->path . DIRECTORY_SEPARATOR . 'composer.json');
            if ($metaData === false) {
                throw new ConfigurationError('Library at "%s" is not a composerized project', $this->path);
            }

            $this->metaData = Json::decode($metaData, true);
        }

        return $this->metaData;
    }

    /**
     * Register and return this library's assets
     *
     * @return array
     */
    protected function assets()
    {
        if ($this->assets !== null) {
            return $this->assets;
        }

        $listAssets = function ($type) {
            $dir = join(DIRECTORY_SEPARATOR, [$this->path, 'asset', $type]);
            if (! is_dir($dir)) {
                return [];
            }

            $this->{$type . 'AssetPath'} = $dir;

            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(
                $dir,
                RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | RecursiveDirectoryIterator::SKIP_DOTS
            ));
            if ($type === 'static') {
                return $iterator;
            }

            return new CallbackFilterIterator(
                $iterator,
                function ($path) use ($type) {
                    if ($type === 'js' && $path->getExtension() === 'js') {
                        return substr($path->getPathname(), -5 - strlen($type)) !== ".min.$type";
                    } elseif ($type === 'css'
                        && ($path->getExtension() === 'css' || $path->getExtension() === 'less')
                    ) {
                        return substr($path->getPathname(), -5 - strlen($type)) !== ".min.$type";
                    }

                    return false;
                }
            );
        };

        $this->assets = [];

        $jsAssets = $listAssets('js');
        $this->assets['js'] = is_array($jsAssets) ? $jsAssets : iterator_to_array($jsAssets);

        $cssAssets = $listAssets('css');
        $this->assets['css'] = is_array($cssAssets) ? $cssAssets : iterator_to_array($cssAssets);

        $staticAssets = $listAssets('static');
        $this->assets['static'] = is_array($staticAssets) ? $staticAssets : iterator_to_array($staticAssets);

        return $this->assets;
    }
}