1 | <?php |
||
34 | 1 | abstract class BaseControl extends Application\UI\Control |
|
35 | { |
||
36 | const TEMPLATE_LAYOUT = 'layout'; |
||
37 | const TEMPLATE_CONFIRMER = 'template'; |
||
38 | |||
39 | /** |
||
40 | * @var string|NULL |
||
41 | */ |
||
42 | protected $templateFile = NULL; |
||
43 | |||
44 | /** |
||
45 | * @var string|NULL |
||
46 | */ |
||
47 | protected $layoutFile = NULL; |
||
48 | |||
49 | /** |
||
50 | * @var Localization\ITranslator |
||
51 | */ |
||
52 | protected $translator; |
||
53 | |||
54 | /** |
||
55 | * @param Localization\ITranslator $translator |
||
56 | */ |
||
57 | public function injectTranslator(Localization\ITranslator $translator = NULL) |
||
61 | |||
62 | /** |
||
63 | * Change default control template path |
||
64 | * |
||
65 | * @param string $templateFile |
||
66 | * @param string $type |
||
67 | * |
||
68 | * @return void |
||
69 | * |
||
70 | * @throws Exceptions\FileNotFoundException |
||
71 | * @throws Exceptions\InvalidArgumentException |
||
72 | */ |
||
73 | public function setTemplateFilePath(string $templateFile, string $type) |
||
74 | { |
||
75 | 1 | if (!in_array($type, [self::TEMPLATE_CONFIRMER, self::TEMPLATE_LAYOUT])) { |
|
76 | throw new Exceptions\InvalidArgumentException('Wrong template type'); |
||
77 | } |
||
78 | |||
79 | // Check if template file exists... |
||
80 | 1 | if (!is_file($templateFile)) { |
|
81 | // Get component actual dir |
||
82 | 1 | $dir = dirname($this->getReflection()->getFileName()); |
|
83 | |||
84 | // ...check if extension template is used |
||
85 | 1 | if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) { |
|
86 | 1 | $templateFile = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile; |
|
87 | |||
88 | 1 | } elseif (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile . '.latte')) { |
|
89 | $templateFile = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile . '.latte'; |
||
90 | |||
91 | } else { |
||
92 | // ...if not throw exception |
||
93 | 1 | throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile)); |
|
94 | } |
||
95 | } |
||
96 | |||
97 | 1 | if ($type == self::TEMPLATE_LAYOUT) { |
|
98 | $this->layoutFile = $templateFile; |
||
99 | |||
100 | } else { |
||
101 | 1 | $this->templateFile = $templateFile; |
|
102 | } |
||
103 | 1 | } |
|
104 | |||
105 | /** |
||
106 | * @param Localization\ITranslator $translator |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public function setTranslator(Localization\ITranslator $translator) |
||
111 | { |
||
112 | $this->translator = $translator; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return Localization\ITranslator|NULL |
||
117 | */ |
||
118 | public function getTranslator() |
||
119 | { |
||
120 | 1 | if ($this->translator instanceof Localization\ITranslator) { |
|
121 | return $this->translator; |
||
122 | } |
||
123 | |||
124 | 1 | return NULL; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Render control |
||
129 | * |
||
130 | * @return Application\UI\ITemplate |
||
131 | * |
||
132 | * @throws Exceptions\InvalidStateException |
||
133 | */ |
||
134 | public function render() |
||
149 | } |
||
150 |