UrlHelper::_findPluginAsset()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 2
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\View\Helper;
17
18
use JBZoo\Utils\FS;
19
use Core\Core\Plugin;
20
use Cake\Core\Configure;
21
use Cake\View\Helper\UrlHelper as CakeUrlHelper;
22
23
/**
24
 * Class UrlHelper
25
 *
26
 * @package Core\View\Helper
27
 */
28
class UrlHelper extends CakeUrlHelper
29
{
30
31
    /**
32
     * Get absolute asset path.
33
     *
34
     * @param string $source Plugin.path/to/file.css
35
     * @param null|string $type Assets folder - default is file ext
36
     * @return bool|string
37
     */
38
    public function assetPath($source, $type = null)
39
    {
40
        $ext  = FS::ext($source);
41
        $type = (empty($type)) ? $ext : $type;
42
43
        $path = FS::clean(WWW_ROOT . '/' . $source, '/');
44
        if (FS::isFile($path)) {
45
            return $path;
46
        }
47
48
        $path = FS::clean(WWW_ROOT . '/' . $type . '/' . $source, '/');
49
        if (FS::isFile($path)) {
50
            return $path;
51
        }
52
53
        $path = $this->_findPluginAsset($source, $type);
54
55
        return $path;
56
    }
57
58
    /**
59
     * Find plugin assets by source.
60
     *
61
     * @param string $source
62
     * @param null|string $type
63
     * @return bool|string
64
     */
65
    protected function _findPluginAsset($source, $type = null)
66
    {
67
        list($plugin, $path) = pluginSplit($source);
68
        $plugin = (string) $plugin;
69
        if (Plugin::isLoaded($plugin)) {
70
            $plgPath = implode('/', [Plugin::path($plugin), Configure::read('App.webroot'), $type, $path]);
71
            $plgPath = FS::clean($plgPath, '/');
72
            if (FS::isFile($plgPath)) {
73
                return $plgPath;
74
            }
75
        }
76
77
        return false;
78
    }
79
}
80