Completed
Push — master ( 9c0bb9...bbec20 )
by Cheren
02:32
created

Theme::setup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core;
17
18
use Cake\Core\App;
19
use JBZoo\Utils\FS;
20
use Cake\Core\Configure;
21
22
/**
23
 * Class Theme
24
 *
25
 * @package Core
26
 */
27
class Theme extends Plugin
28
{
29
30
    /**
31
     * Setup current theme.
32
     *
33
     * @param null|string $prefix
34
     * @return null|string
35
     */
36
    public static function setup($prefix = null)
37
    {
38
        $theme = self::name($prefix);
39
        $path  = self::find($theme);
40
41
        if ($path !== null) {
42
            $config = self::getData($theme, 'meta');
43
            if ($config->get('type') == 'theme') {
44
                return $theme;
45
            }
46
        }
47
48
        return null;
49
    }
50
51
    /**
52
     * Get name by prefix.
53
     *
54
     * @param string|null $prefix
55
     * @return mixed
56
     */
57
    public static function name($prefix = null)
58
    {
59
        return ($prefix == 'admin') ? Configure::read('Theme.admin') : Configure::read('Theme.site');
60
    }
61
62
    /**
63
     * Find theme plugin in path.
64
     *
65
     * @param string $theme
66
     * @return null|string
67
     */
68
    public static function find($theme)
69
    {
70
        $paths = App::path('Plugin');
71
        foreach ($paths as $path) {
72
            $path = FS::clean($path . '/', DS);
73
            $themeFolder = $path . $theme;
74
75
            if (FS::isDir($themeFolder)) {
76
                return $themeFolder;
77
            }
78
        }
79
80
        return Configure::read('plugins.' . $theme);
81
    }
82
}
83