|
1
|
|
|
<?php |
|
2
|
|
|
namespace vsc\application\sitemaps; |
|
3
|
|
|
|
|
4
|
|
|
use vsc\ExceptionPath; |
|
5
|
|
|
use vsc\presentation\views\ViewA; |
|
6
|
|
|
|
|
7
|
|
|
trait ControllerMapTrait { |
|
8
|
|
|
private $sMainTemplatePath; |
|
9
|
|
|
private $sMainTemplate; |
|
10
|
|
|
|
|
11
|
|
|
private $sViewPath; |
|
12
|
|
|
private $oView; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @return ModuleMap |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract public function getModuleMap(); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $sPath |
|
21
|
|
|
* @return bool |
|
22
|
|
|
* @throws \vsc\ExceptionPath |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function setMainTemplatePath($sPath) { |
|
25
|
3 |
|
$sMainTemplatePath = realpath($sPath); |
|
26
|
3 |
|
if (!is_dir($sMainTemplatePath)) { |
|
27
|
2 |
|
$sMainTemplatePath = realpath($this->getModuleMap()->getModulePath() . DIRECTORY_SEPARATOR . $sPath); |
|
28
|
|
|
} |
|
29
|
3 |
|
if (!is_dir($sMainTemplatePath)) { |
|
30
|
|
|
throw new ExceptionPath(sprintf('Path [%s] does not exist', $sPath)); |
|
31
|
|
|
} |
|
32
|
3 |
|
$this->sMainTemplatePath = $sMainTemplatePath; |
|
33
|
|
|
|
|
34
|
3 |
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
21 |
|
public function getMainTemplatePath() { |
|
41
|
|
|
// if we didn't provide the controller with a main template path we check the module |
|
42
|
21 |
|
if (is_null($this->sMainTemplatePath)) { |
|
43
|
19 |
|
if ($this->getModuleMap() instanceof ContentTypeMappingInterface) { |
|
44
|
19 |
|
$this->sMainTemplatePath = $this->getModuleMap()->getMainTemplatePath(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
21 |
|
if (is_null($this->sMainTemplatePath)) { |
|
49
|
|
|
// back-up |
|
50
|
19 |
|
$this->sMainTemplatePath = VSC_RES_PATH . 'templates'; |
|
51
|
|
|
} |
|
52
|
21 |
|
if (substr($this->sMainTemplatePath, -1) != DIRECTORY_SEPARATOR) { |
|
53
|
21 |
|
$this->sMainTemplatePath .= DIRECTORY_SEPARATOR; |
|
54
|
|
|
} |
|
55
|
21 |
|
return $this->sMainTemplatePath; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $sPath |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function setMainTemplate($sPath) { |
|
62
|
1 |
|
$this->sMainTemplate = $sPath; |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
21 |
|
public function getMainTemplate() { |
|
69
|
|
|
// if we didn't provide the controller with a main template path we check the module |
|
70
|
21 |
|
if (is_null($this->sMainTemplate)) { |
|
71
|
20 |
|
if ($this->getModuleMap() instanceof ContentTypeMappingInterface) { |
|
72
|
20 |
|
$this->sMainTemplate = $this->getModuleMap()->getMainTemplate(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
21 |
|
if (is_null($this->sMainTemplate)) { |
|
76
|
|
|
// back-up |
|
77
|
20 |
|
$this->sMainTemplate = 'main.php'; |
|
78
|
|
|
} |
|
79
|
21 |
|
return $this->sMainTemplate; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* |
|
84
|
|
|
* To allow a single controller type to return a different type of view |
|
85
|
|
|
* @param string|object $mView |
|
86
|
|
|
* @throws ExceptionPath |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function setView($mView) { |
|
89
|
3 |
|
if (ViewA::isValid($mView)) { |
|
90
|
|
|
$this->oView = $mView; |
|
|
|
|
|
|
91
|
|
|
$this->sViewPath = get_class($mView); |
|
92
|
3 |
|
} elseif (ClassMap::isValidMap($mView)) { |
|
|
|
|
|
|
93
|
3 |
|
$this->sViewPath = $mView; |
|
94
|
|
|
} else { |
|
95
|
|
|
throw new ExceptionPath('View path [' . $mView . '] is not valid.'); |
|
96
|
|
|
} |
|
97
|
3 |
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
public function getViewPath() { |
|
|
|
|
|
|
100
|
3 |
|
return $this->sViewPath; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
6 |
|
public function getView() { |
|
|
|
|
|
|
104
|
6 |
|
if (!ViewA::isValid($this->oView) && !is_null($this->sViewPath)) { |
|
105
|
3 |
|
if (stristr(basename($this->sViewPath), '.') === false && !is_file($this->sViewPath)) { |
|
106
|
3 |
|
$sClassName = $this->sViewPath; |
|
107
|
|
|
} elseif (is_file($this->sViewPath)) { |
|
108
|
|
|
$sViewPath = $this->getViewPath(); |
|
109
|
|
|
try { |
|
110
|
|
|
include ($sViewPath); |
|
111
|
|
|
} catch (\Exception $e) { |
|
112
|
|
|
\vsc\_e($e); |
|
113
|
|
|
} |
|
114
|
|
|
$sClassName = SiteMapA::getClassName($sViewPath); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
6 |
|
if (!empty($sClassName)) { |
|
118
|
3 |
|
$this->oView = new $sClassName(); |
|
119
|
|
|
} |
|
120
|
6 |
|
return $this->oView; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.