Completed
Push — master ( 9afd96...42fd87 )
by Cheren
09:16
created

Manager::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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\Helper;
17
18
use Cake\Core\Configure;
19
use Core\Plugin;
20
use Core\Container;
21
use JBZoo\Utils\Arr;
22
use JBZoo\Utils\Str;
23
use Cake\Utility\Inflector;
24
25
/**
26
 * Class Manager
27
 *
28
 * @package Core\Helper
29
 */
30
class Manager extends Container
31
{
32
33
    /**
34
     * Helper class name suffix.
35
     */
36
    const HELPER_SUFFIX = 'Helper';
37
38
    /**
39
     * Hold loaded helpers.
40
     *
41
     * @var array
42
     */
43
    protected static $_loaded = [];
44
45
    /**
46
     * List of namespaces.
47
     *
48
     * @var array
49
     */
50
    protected static $_namespace = [];
51
52
    /**
53
     * Manager constructor.
54
     *
55
     * @param array $values
56
     */
57
    public function __construct(array $values = [])
58
    {
59
        parent::__construct($values);
60
        foreach ((array) Plugin::loaded() as $plugin) {
61
            $this->addNamespace($plugin);
62
        }
63
    }
64
65
    /**
66
     * Add new helper group namespace.
67
     *
68
     * @param string $name
69
     * @return bool
70
     */
71
    public function addNamespace($name)
72
    {
73
        if (!Arr::in($name, self::$_namespace)) {
74
            self::$_namespace[] = $name;
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * Gets a parameter or an object.
83
     *
84
     * @param string $id
85
     * @return mixed
86
     */
87
    public function offsetGet($id)
88
    {
89
        $id = Str::low($id);
90
        if (!Arr::key($id, self::$_loaded)) {
91
            $className = $this->_getClassName($id);
92
            $this->_register($id, $className);
93
        }
94
95
        return parent::offsetGet($id);
96
    }
97
98
    /**
99
     * Register helper class.
100
     *
101
     * @param string $id
102
     * @param $className
103
     */
104
    protected function _register($id, $className)
105
    {
106
        $id = (string) $id;
107
        if (class_exists($className)) {
108
            self::$_loaded[$id] = $className;
109
            $this[$id] = function () use ($className) {
110
                return new $className();
111
            };
112
        } else {
113
            throw new Exception("Helper \"{{$className}}\" not found!");
114
        }
115
    }
116
117
    /**
118
     * Get current helper class name.
119
     *
120
     * @param string $class
121
     * @return string
122
     */
123
    protected function _getClassName($class)
124
    {
125
        $class = Str::low($class);
126
        list ($plugin, $className) = pluginSplit($class);
127
        $return = self::HELPER_SUFFIX . '\\' . Inflector::camelize($className) . self::HELPER_SUFFIX;
128
        if ($plugin !== null) {
129
            return Inflector::camelize($plugin) . '\\' . $return;
130
        }
131
132
        return Configure::read('App.namespace') . '\\' . $return;
133
    }
134
}
135