1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\App\Template; |
4
|
|
|
|
5
|
|
|
// From 'psr/log' |
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
7
|
|
|
use Psr\Log\LoggerAwareTrait; |
8
|
|
|
|
9
|
|
|
// From 'psr/http-message' |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
|
12
|
|
|
// From 'pimple/pimple' |
13
|
|
|
use Pimple\Container; |
14
|
|
|
|
15
|
|
|
// From 'charcoal-config' |
16
|
|
|
use Charcoal\Config\AbstractEntity; |
17
|
|
|
|
18
|
|
|
// From 'charcoal-app' |
19
|
|
|
use Charcoal\App\Template\TemplateInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Template (View Controller) base class |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractTemplate extends AbstractEntity implements |
25
|
|
|
LoggerAwareInterface, |
26
|
|
|
TemplateInterface |
27
|
|
|
{ |
28
|
|
|
use LoggerAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The cache of parsed template names. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected static $templateNameCache = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array|\ArrayAccess $data The dependencies (app and logger). |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function __construct($data = null) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$this->setLogger($data['logger']); |
43
|
|
|
|
44
|
|
|
if (isset($data['container'])) { |
45
|
|
|
$this->setDependencies($data['container']); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Retrieve the template's identifier. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function templateName() |
55
|
|
|
{ |
56
|
|
|
$key = substr(strrchr('\\'.get_class($this), '\\'), 1); |
57
|
|
|
|
58
|
|
|
if (!isset(static::$templateNameCache[$key])) { |
59
|
|
|
$value = $key; |
60
|
|
|
|
61
|
|
|
if (!ctype_lower($value)) { |
62
|
|
|
$value = preg_replace('/\s+/u', '', $value); |
63
|
|
|
$value = mb_strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1-', $value), 'UTF-8'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$value = str_replace( |
67
|
|
|
[ 'abstract', 'trait', 'interface', 'template', '\\' ], |
68
|
|
|
'', |
69
|
|
|
$value |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
static::$templateNameCache[$key] = trim($value, '-'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return static::$templateNameCache[$key]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Initialize the template with a request. |
80
|
|
|
* |
81
|
|
|
* @param RequestInterface $request The request to intialize. |
82
|
|
|
* @return boolean Success / Failure. |
83
|
|
|
*/ |
84
|
|
|
public function init(RequestInterface $request) |
85
|
|
|
{ |
86
|
|
|
// This method is a stub. Reimplement in children methods to ensure template initialization. |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Give an opportunity to children classes to inject dependencies from a Pimple Container. |
92
|
|
|
* |
93
|
|
|
* Does nothing by default, reimplement in children classes. |
94
|
|
|
* |
95
|
|
|
* The `$container` DI-container (from `Pimple`) should not be saved or passed around, only to be used to |
96
|
|
|
* inject dependencies (typically via setters). |
97
|
|
|
* |
98
|
|
|
* @param Container $container A dependencies container instance. |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
protected function setDependencies(Container $container) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
// This method is a stub. Reimplement in children template classes. |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.