Component.php
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* @package Better Preview
* @version 6.1.0
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2018 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace RegularLabs\Plugin\System\BetterPreview;
defined('_JEXEC') or die;
use JFactory;
use JFile;
use RegularLabs\Library\ArrayHelper as RL_Array;
class Component
{
public static function getClass($type = 'Link')
{
if ( ! $namespace = self::getNameSpace($type))
{
return false;
}
return new $namespace;
}
private static function getNameSpace($type = 'Link')
{
$option = JFactory::getApplication()->input->get('option');
$view = JFactory::getApplication()->input->get('view', JFactory::getApplication()->input->get('controller'));
$task = JFactory::getApplication()->input->get('task');
$namespace = 'RegularLabs\\Plugin\\System\\BetterPreview\\';
if (empty($option))
{
return ($type != 'Preview') ? false : $namespace . $type;
}
$option = strlen($option) > 4 && substr($option, 0, 4) == 'com_' ? substr($option, 4) : $option;
$parts = [ucfirst($option), ucfirst($view), ucfirst($task)];
$parts = RL_Array::clean($parts);
while ( ! empty($parts))
{
$last = end($parts);
if (empty($last))
{
array_pop($parts);
continue;
}
$file = __DIR__ . '/Component/' . implode('/', $parts) . '/' . $type . '.php';
if (JFile::exists($file))
{
return $namespace . 'Component\\' . implode('\\', $parts) . '\\' . $type;
}
array_pop($parts);
}
return ($type != 'Preview') ? false : $namespace . $type;
}
}