Completed
Pull Request — master (#16)
by Sergii
07:37
created

JavaScript::getJavaScriptFileContents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils;
6
7
/**
8
 * Trait JavaScript.
9
 *
10
 * @package Drupal\TqExtension\Utils
11
 */
12
trait JavaScript
13
{
14
    /**
15
     * @param string $filename
16
     *   Name of file in "src/JavaScript" without ".js" extension.
17
     *
18
     * @return string
19
     *   Contents of file.
20
     */
21
    protected static function getJavaScriptFileContents($filename)
22
    {
23
        static $files = [];
24
25
        if (empty($files[$filename])) {
26
            $path = str_replace('Utils', 'JavaScript', __DIR__) . "/$filename.js";
27
28
            if (!file_exists($path)) {
29
                throw new \RuntimeException(sprintf('File "%s" does not exists!', $path));
30
            }
31
32
            $files[$filename] = file_get_contents($path);
33
        }
34
35
        return $files[$filename];
36
    }
37
}
38