-
Notifications
You must be signed in to change notification settings - Fork 2
/
Controller.php
99 lines (81 loc) · 2.81 KB
/
Controller.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Copyright (c) 2018. Alexandr Kosarev, @kosarev.by
*/
namespace Joomplace\X;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomplace\X\Helper\Injector;
class Controller extends BaseController
{
use Injector;
protected $_namespace = null;
public function __construct(
array $config = array(),
MVCFactoryInterface $factory = null,
$app = null,
$input = null
) {
$this->_namespace = $config['namespace'];
parent::__construct($config, $factory, $app, $input);
$this->input = $this->getInput();
}
public function execute($task = null)
{
$this->task = $task;
$task = strtolower($task);
if (isset($this->taskMap[$task]))
{
$task = $this->taskMap[$task];
}
elseif (isset($this->taskMap['__default']))
{
$task = $this->taskMap['__default'];
}
else
{
throw new \Exception(\JText::sprintf('JLIB_APPLICATION_ERROR_TASK_NOT_FOUND', $task), 404);
}
if ($this->methodExists($task)) {
$arguments = array();
foreach ($this->getArgs($task) as $param) {
/** @var \ReflectionParameter $param */
/*
* TODO: set initial filter from config
*/
$filter = null;
if ($param->isArray()) {
$filter = 'array';
}
if($param->isDefaultValueAvailable()){
$value = $this->injectArg($param->name, $param->getDefaultValue(), $filter, null);
}else{
$value = $this->injectRequiredArg($param->name, $filter, null);
}
$arguments[] = $value;
}
echo call_user_func_array(array($this, $task), $arguments);
} else {
// TODO: improve
throw new \Exception("Method `" . $this->getClassName() . '::' . $task . "` doesn't exist");
}
}
public function index($cachable = false, $urlparams = array())
{
$view = $this->getView();
return $view->render();
}
protected function createView($name, $prefix = '', $type = '', $config = array())
{
if (!isset($config['base_path'])) {
$config['base_path'] = $this->basePath;
}
$document = \JFactory::getDocument();
$type = $document->getType();
$viewClass = $this->_namespace . ucfirst($this->app->getName()) . '\\View' .
($prefix ? '\\' . ucfirst($prefix) : '') . '\\'. ucfirst($name) .
'\\' . ucfirst($type);
$view = new $viewClass(['name' => $this->name . ':' . $this->input->get('layout', $this->task, 'cmd')]);
return $view;
}
}