forked from forkcms/forkcms
/
Action.php
128 lines (107 loc) · 3.98 KB
/
Action.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Backend\Core\Engine;
/*
* This file is part of Fork CMS.
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/
use Symfony\Component\HttpKernel\KernelInterface;
use Backend\Core\Engine\Base\Action as BackendBaseAction;
/**
* This class is the real code, it creates an action, loads the config file, ...
*
* @author Tijs Verkoyen <tijs@sumocoders.be>
* @author Davy Hellemans <davy.hellemans@netlash.com>
* @author Dave Lens <dave.lens@wijs.be>
*/
class Action extends Base\Object
{
/**
* The config file
*
* @var Base\Config
*/
private $config;
/**
* BackendTemplate
*
* @var Template
*/
public $tpl;
/**
* You have to specify the action and module so we know what to do with this instance
*
* @param KernelInterface $kernel
*/
public function __construct(KernelInterface $kernel)
{
parent::__construct($kernel);
// grab stuff from the reference and store them in this object (for later/easy use)
$this->tpl = $this->getContainer()->get('template');
}
/**
* Execute the action
* We will build the classname, require the class and call the execute method.
*/
public function execute()
{
$this->loadConfig();
// is the requested action possible? If not we throw an exception.
// We don't redirect because that could trigger a redirect loop
if (!in_array($this->getAction(), $this->config->getPossibleActions())) {
throw new Exception('This is an invalid action (' . $this->getAction() . ').');
}
// build action-class
$actionClass = 'Backend\\Modules\\' . $this->getModule() . '\\Actions\\' . $this->getAction();
if ($this->getModule() == 'Core') {
$actionClass = 'Backend\\Core\\Actions\\' . $this->getAction();
}
if (!class_exists($actionClass)) {
throw new Exception('The class ' . $actionClass . ' could not be found.');
}
// get working languages
$languages = Language::getWorkingLanguages();
$workingLanguages = array();
// loop languages and build an array that we can assign
foreach ($languages as $abbreviation => $label) {
$workingLanguages[] = array(
'abbr' => $abbreviation,
'label' => $label,
'selected' => ($abbreviation == Language::getWorkingLanguage())
);
}
// assign the languages
$this->tpl->assign('workingLanguages', $workingLanguages);
// create action-object
/** @var $object BackendBaseAction */
$object = new $actionClass($this->getKernel());
$this->getContainer()->get('logger')->info(
"Executing backend action '{$object->getAction()}' for module '{$object->getModule()}'."
);
$object->execute();
return $object->getContent();
}
/**
* Load the config file for the requested module.
* In the config file we have to find disabled actions, the constructor
* will read the folder and set possible actions
* Other configurations will be stored in it also.
*/
public function loadConfig()
{
// check if we can load the config file
$configClass = 'Backend\\Modules\\' . $this->getModule() . '\\Config';
if ($this->getModule() == 'Core') {
$configClass = 'Backend\\Core\\Config';
}
// validate if class exists (aka has correct name)
if (!class_exists($configClass)) {
throw new Exception('The config file ' . $configClass . ' could not be found.');
}
// create config-object, the constructor will do some magic
$this->config = new $configClass($this->getKernel(), $this->getModule());
// set action
$action = ($this->config->getDefaultAction() !== null) ? $this->config->getDefaultAction() : 'Index';
}
}