1 | <?php |
||
11 | class Name |
||
12 | { |
||
13 | const NAMESPACE_DELIMITER = '::'; |
||
14 | |||
15 | /** |
||
16 | * Instance of the template engine. |
||
17 | * @var Engine |
||
18 | */ |
||
19 | protected $engine; |
||
20 | |||
21 | /** |
||
22 | * The original name. |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $name; |
||
26 | |||
27 | /** |
||
28 | * The parsed namespace |
||
29 | */ |
||
30 | protected $namespace; |
||
31 | |||
32 | protected $folder; |
||
33 | |||
34 | /** |
||
35 | * The parsed template path. |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $path; |
||
39 | |||
40 | /** |
||
41 | * Create a new Name instance. |
||
42 | 100 | * @param Engine $engine |
|
43 | * @param string $name |
||
44 | 100 | */ |
|
45 | 100 | public function __construct(Engine $engine, $name) |
|
46 | 94 | { |
|
47 | $this->setEngine($engine); |
||
48 | $this->setName($name); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Set the engine. |
||
53 | 100 | * @param Engine $engine |
|
54 | * @return Name |
||
55 | 100 | */ |
|
56 | public function setEngine(Engine $engine) |
||
57 | 100 | { |
|
58 | $this->engine = $engine; |
||
59 | |||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | 2 | * Get the engine. |
|
65 | * @return Engine |
||
66 | 2 | */ |
|
67 | public function getEngine() |
||
68 | { |
||
69 | return $this->engine; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Set the original name and parse it. |
||
74 | 100 | * @param string $name |
|
75 | * @return Name |
||
76 | 100 | */ |
|
77 | public function setName($name) |
||
78 | 100 | { |
|
79 | $this->name = $name; |
||
80 | 100 | ||
81 | 88 | $parts = explode(static::NAMESPACE_DELIMITER, $this->name); |
|
82 | 98 | ||
83 | 10 | if (count($parts) === 1) { |
|
84 | 10 | $this->setPath($parts[0]); |
|
85 | 8 | } elseif (count($parts) === 2) { |
|
86 | 2 | $this->setNamespace($parts[0]); |
|
87 | 2 | $this->setPath($parts[1]); |
|
88 | } else { |
||
89 | 2 | throw new LogicException( |
|
90 | 'The template name "' . $this->name . '" is not valid. ' . |
||
91 | 'Do not use the folder namespace separator "::" more than once.' |
||
92 | 94 | ); |
|
93 | } |
||
94 | |||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | 10 | * Get the original name. |
|
100 | * @return string |
||
101 | 10 | */ |
|
102 | public function getName() |
||
103 | { |
||
104 | return $this->name; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Set the parsed template folder. |
||
109 | 10 | * @param string $namespace |
|
110 | * @return Name |
||
111 | 10 | */ |
|
112 | public function setNamespace($namespace) |
||
113 | 10 | { |
|
114 | $this->namespace = $namespace; |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | 8 | * Get the parsed template folder. |
|
121 | * @return string |
||
122 | 8 | */ |
|
123 | public function getNamespace() |
||
124 | { |
||
125 | return $this->namespace; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @deprecated |
||
130 | 98 | */ |
|
131 | public function getFolder() |
||
132 | 98 | { |
|
133 | 4 | if ($this->getNamespace()) { |
|
134 | 4 | return $this->getEngine()->getFolders()->get($this->getNamespace()); |
|
135 | } |
||
136 | 4 | return null; |
|
137 | } |
||
138 | |||
139 | 94 | /** |
|
140 | * Set the parsed template file. |
||
141 | 94 | * @param string $path |
|
142 | 92 | * @return Name |
|
143 | 92 | */ |
|
144 | public function setPath($path) |
||
145 | 94 | { |
|
146 | if ($path === '') { |
||
147 | throw new LogicException( |
||
148 | 'The template name "' . $this->name . '" is not valid. ' . |
||
149 | 'The template name cannot be empty.' |
||
150 | ); |
||
151 | } |
||
152 | 8 | ||
153 | $this->path = $path; |
||
154 | 8 | ||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | public function getFile() |
||
159 | { |
||
160 | $file = $this->path; |
||
161 | 72 | if (!is_null($this->getEngine()->getFileExtension())) { |
|
162 | $file .= '.'.$this->getEngine()->getFileExtension(); |
||
163 | 72 | } |
|
164 | 68 | return $file; |
|
165 | } |
||
166 | |||
167 | 4 | /** |
|
168 | * Resolve template path or |
||
169 | 4 | * Get the parsed template file. |
|
170 | 2 | * @return string |
|
171 | 2 | */ |
|
172 | public function getPath($resolve = true ) |
||
173 | 4 | { |
|
174 | if ($resolve) { |
||
175 | return $this->engine->path($this); |
||
176 | } |
||
177 | return $this->path; |
||
178 | } |
||
179 | |||
180 | 60 | /** |
|
181 | * Check if template path exists. |
||
182 | 60 | * @return boolean |
|
183 | */ |
||
184 | public function doesPathExist() |
||
188 | } |
||
189 |