Completed
Push — master ( f8c69d...31fd24 )
by Neomerx
01:36
created

TemplatesSettings::getTemplateNames()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 1
nop 2
crap 4
1
<?php namespace Limoncello\Templates\Package;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A;
20
use Limoncello\Contracts\Settings\SettingsInterface;
21
use RecursiveDirectoryIterator;
22
use RecursiveIteratorIterator;
23
use SplFileInfo;
24
25
/**
26
 * @package Limoncello\Templates
27
 */
28
class TemplatesSettings implements SettingsInterface
29
{
30
    /** Settings key */
31
    const KEY_TEMPLATES_FOLDER = 0;
32
33
    /** Settings key */
34
    const KEY_TEMPLATES_FILE_MASK = self::KEY_TEMPLATES_FOLDER + 1;
35
36
    /** Settings key */
37
    const KEY_CACHE_FOLDER = self::KEY_TEMPLATES_FILE_MASK + 1;
38
39
    /** Settings key */
40
    const KEY_APP_ROOT_FOLDER = self::KEY_CACHE_FOLDER + 1;
41
42
    /** Settings key */
43
    const KEY_IS_DEBUG = self::KEY_APP_ROOT_FOLDER + 1;
44
45
    /** Settings key */
46
    const KEY_IS_AUTO_RELOAD = self::KEY_IS_DEBUG + 1;
47
48
    /** Settings key */
49
    const KEY_TEMPLATES_LIST = self::KEY_IS_AUTO_RELOAD + 1;
50
51
    /** Settings key */
52
    protected const KEY_LAST = self::KEY_TEMPLATES_LIST;
53
54
    /**
55
     * @var array
56
     */
57
    private $appConfig;
58
59
    /**
60
     * @inheritdoc
61
     */
62 4
    final public function get(array $appConfig): array
63
    {
64 4
        $this->appConfig = $appConfig;
65
66 4
        $defaults = $this->getSettings();
67
68 4
        $templatesFolder   = $defaults[static::KEY_TEMPLATES_FOLDER] ?? null;
69 4
        $templatesFileMask = $defaults[static::KEY_TEMPLATES_FILE_MASK] ?? null;
70 4
        $cacheFolder       = $defaults[static::KEY_CACHE_FOLDER] ?? null;
71 4
        $appRootFolder     = $defaults[static::KEY_APP_ROOT_FOLDER] ?? null;
72
73 4
        assert(
74 4
            $templatesFolder !== null && empty(glob($templatesFolder)) === false,
75 4
            "Invalid Templates folder `$templatesFolder`."
76
        );
77 4
        assert(empty($templatesFileMask) === false, "Invalid Templates file mask `$templatesFileMask`.");
78 4
        assert(
79 4
            $cacheFolder !== null && empty(glob($cacheFolder)) === false,
80 4
            "Invalid Cache folder `$cacheFolder`."
81
        );
82 4
        assert(
83 4
            $appRootFolder !== null && empty(glob($appRootFolder)) === false,
84 4
            "Invalid App root folder `$appRootFolder`."
85
        );
86
87 4
        $realTemplatesFolder = realpath($templatesFolder);
88 4
        $realCacheFolder     = realpath($cacheFolder);
89 4
        $realAppRootFolder   = realpath($appRootFolder);
90
91 4
        $defaults[static::KEY_TEMPLATES_FOLDER] = $realTemplatesFolder;
92 4
        $defaults[static::KEY_CACHE_FOLDER]     = $realCacheFolder;
93 4
        $defaults[static::KEY_APP_ROOT_FOLDER]  = $realAppRootFolder;
94
95
        return $defaults + [
96 4
                static::KEY_TEMPLATES_LIST => $this->getTemplateNames($realTemplatesFolder, $templatesFileMask),
97
            ];
98
    }
99
100
    /**
101
     * @return array
102
     */
103 4
    protected function getSettings(): array
104
    {
105 4
        $appConfig = $this->getAppConfig();
106
107 4
        $isDebug = (bool)($appConfig[A::KEY_IS_DEBUG] ?? false);
108
109
        return [
110 4
            static::KEY_IS_DEBUG            => $isDebug,
111 4
            static::KEY_IS_AUTO_RELOAD      => $isDebug,
112 4
            static::KEY_TEMPLATES_FILE_MASK => '*.twig',
113
        ];
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119 4
    protected function getAppConfig()
120
    {
121 4
        return $this->appConfig;
122
    }
123
124
    /**
125
     * @param string $templatesFolder
126
     * @param string $templatesFileMask
127
     *
128
     * @return array
129
     */
130
    private function getTemplateNames(string $templatesFolder, string $templatesFileMask): array
131
    {
132 4
        return iterator_to_array(call_user_func(function () use ($templatesFolder, $templatesFileMask) {
133
            $flags    =
134
                RecursiveDirectoryIterator::SKIP_DOTS |
135
                RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
136 4
                RecursiveDirectoryIterator::CURRENT_AS_FILEINFO;
137 4
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($templatesFolder, $flags));
138 4
            foreach ($iterator as $found) {
139
                /** @var SplFileInfo $found */
140 4
                if ($found->isFile() === true && fnmatch($templatesFileMask, $found->getFilename()) === true) {
141 4
                    $fullFileName = $found->getPath() . DIRECTORY_SEPARATOR . $found->getFilename();
142 4
                    $templateName = str_replace($templatesFolder . DIRECTORY_SEPARATOR, '', $fullFileName);
143 4
                    yield $templateName;
144
                }
145
            }
146 4
        }));
147
    }
148
}
149