| 1 | <?php |
||
| 2 | |||
| 3 | // ------------------------------------------------------------------------- |
||
| 4 | // OVIDENTIA http://www.ovidentia.org |
||
| 5 | // Ovidentia is free software; you can redistribute it and/or modify |
||
| 6 | // it under the terms of the GNU General Public License as published by |
||
| 7 | // the Free Software Foundation; either version 2, or (at your option) |
||
| 8 | // any later version. |
||
| 9 | // |
||
| 10 | // This program is distributed in the hope that it will be useful, but |
||
| 11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
| 13 | // See the GNU General Public License for more details. |
||
| 14 | // |
||
| 15 | // You should have received a copy of the GNU General Public License |
||
| 16 | // along with this program; if not, write to the Free Software |
||
| 17 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
||
| 18 | // USA. |
||
| 19 | // ------------------------------------------------------------------------- |
||
| 20 | /** |
||
| 21 | * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL) |
||
| 22 | * @copyright Copyright (c) 2022 by SI4YOU ({@link https://www.siforyou.com}) |
||
| 23 | */ |
||
| 24 | namespace Capwelton\LibApp; |
||
| 25 | |||
| 26 | use Capwelton\LibApp\Ctrl\AppComponentCtrlRecord; |
||
| 27 | use Capwelton\LibApp\Interfaces\AppComponentUi; |
||
| 28 | |||
| 29 | class AppComponent |
||
| 30 | { |
||
| 31 | |||
| 32 | const ENTRY = 'Entry'; |
||
| 33 | |||
| 34 | const COUNTRY = 'Country'; |
||
| 35 | |||
| 36 | const ADDRESS = 'Address'; |
||
| 37 | |||
| 38 | private $app; |
||
| 39 | |||
| 40 | private $set; |
||
| 41 | |||
| 42 | private $controller; |
||
| 43 | |||
| 44 | private $ui; |
||
| 45 | |||
| 46 | private $controllerObject; |
||
| 47 | |||
| 48 | private $uiObject; |
||
| 49 | |||
| 50 | private $packageName; |
||
| 51 | |||
| 52 | public function __construct(Func_App $app, $set = null, $controller = null, $ui = null) |
||
| 53 | { |
||
| 54 | $this->app = $app; |
||
| 55 | $this->set = $set; |
||
| 56 | $this->controller = $controller; |
||
| 57 | $this->ui = $ui; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returns a multidimensional array with information about required and optional components. |
||
| 62 | * The returned array may have 0, 1 or 2 keys : 'requiredComponents' and 'optionalComponents'. |
||
| 63 | * Each of these keys is associated to an array of strings for each component name. |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function checkDependencies() |
||
| 68 | { |
||
| 69 | $main = new \ReflectionClass($this->set); |
||
| 70 | $dependencies = array(); |
||
| 71 | if($main->hasMethod('getRequiredComponents')){ |
||
| 72 | $requiredComponents = $this->recordSet()->getRequiredComponents(); |
||
| 73 | foreach ($requiredComponents as $requiredComponent){ |
||
| 74 | $dependencies['requiredComponents'][] = $requiredComponent; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | if($main->hasMethod('getOptionalComponents')){ |
||
| 78 | $optionalComponents = $this->recordSet()->getOptionalComponents(); |
||
| 79 | foreach ($optionalComponents as $optionalComponent){ |
||
| 80 | $dependencies['optionalComponents'][] = $optionalComponent; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | return $dependencies; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function setPackageName($packageName) |
||
| 88 | { |
||
| 89 | $this->packageName = $packageName; |
||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function getPackageName() |
||
| 94 | { |
||
| 95 | if(! isset($this->packageName) || empty($this->packageName)){ |
||
| 96 | $packageName = ''; |
||
| 97 | $setRc = new \ReflectionClass($this->set); |
||
| 98 | // Tries to automatically retrieve the package name from the definition.json |
||
| 99 | if($setRc){ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 100 | // Get definition file path |
||
| 101 | $definitionPathParts = explode('/', $setRc->getFileName()); |
||
| 102 | $definitionPath = implode('/', (array_splice($definitionPathParts, 0, - 3))); |
||
| 103 | $definitionPath .= '/definition.json'; |
||
| 104 | $definitionContent = file_get_contents($definitionPath); |
||
| 105 | if($definitionContent){ |
||
| 106 | // Get definition.json content |
||
| 107 | $definitionContent = json_decode($definitionContent, true); |
||
| 108 | if($definitionContent && isset($definitionContent['name'])){ |
||
| 109 | // Get name in definition.json |
||
| 110 | $definitionNameParts = explode('/', $definitionContent['name']); |
||
| 111 | $packageName = $definitionNameParts[count($definitionNameParts) - 1]; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->packageName = $packageName; |
||
| 117 | } |
||
| 118 | return $this->packageName; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * If the component record set has a method onUpdate, this method is called. |
||
| 123 | * This method should only be called in the synchronizeComponentsSql method of the addon. |
||
| 124 | */ |
||
| 125 | public function onUpdate() |
||
| 126 | { |
||
| 127 | $main = new \ReflectionClass($this->set); |
||
| 128 | if($main->hasMethod('onUpdate')){ |
||
| 129 | $this->recordSet()->onUpdate(); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return AppComponentCtrlRecord |
||
| 135 | */ |
||
| 136 | public function controller($proxy = true) |
||
| 137 | { |
||
| 138 | if(! isset($this->controllerObject)){ |
||
| 139 | $this->app->includeRecordController(); |
||
| 140 | $ctrl = $this->controller; |
||
| 141 | $this->controllerObject = new $ctrl($this->app, $this); |
||
| 142 | } |
||
| 143 | if($proxy){ |
||
| 144 | return $this->controllerObject->proxy(); |
||
| 145 | } |
||
| 146 | return $this->controllerObject; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return AppComponentCtrlRecord |
||
| 151 | */ |
||
| 152 | public function proxy() |
||
| 153 | { |
||
| 154 | return $this->controller(); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return AppComponentUi|null |
||
| 159 | */ |
||
| 160 | public function ui() |
||
| 161 | { |
||
| 162 | $this->app->includeUi(); |
||
| 163 | if(! isset($this->uiObject)){ |
||
| 164 | $ui = $this->ui; |
||
| 165 | if(! isset($ui)){ |
||
| 166 | return null; |
||
| 167 | } |
||
| 168 | $this->uiObject = new $ui($this->app); |
||
| 169 | } |
||
| 170 | return $this->uiObject; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function recordSet() |
||
| 174 | { |
||
| 175 | $set = $this->set; |
||
| 176 | return new $set($this->app); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function getSetClassName() |
||
| 180 | { |
||
| 181 | $reflectionClass = new \ReflectionClass($this->set); |
||
| 182 | return $this->app->classPrefix . $reflectionClass->getShortName(); |
||
| 183 | } |
||
| 184 | |||
| 185 | public function getRecordClassName() |
||
| 186 | { |
||
| 187 | return substr($this->getSetClassName(), 0, - 3); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function getName() |
||
| 191 | { |
||
| 192 | $reflectionClass = new \ReflectionClass($this->set); |
||
| 193 | return substr($reflectionClass->getShortName(), 0, - 3); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function getDefinition() |
||
| 197 | { |
||
| 198 | $componentDefinitionObjectName = 'Capwelton\App\\' . $this->getPackageName() . '\ComponentDefinition\ComponentDefinition'; |
||
| 199 | if(class_exists($componentDefinitionObjectName)){ |
||
| 200 | return new $componentDefinitionObjectName(); |
||
| 201 | } |
||
| 202 | return null; |
||
| 203 | } |
||
| 204 | |||
| 205 | public function getLangPath() |
||
| 206 | { |
||
| 207 | $def = $this->getDefinition(); |
||
| 208 | if($def){ |
||
| 209 | return $def->getLangPath($this->app); |
||
| 210 | } |
||
| 211 | return null; |
||
| 212 | } |
||
| 213 | |||
| 214 | public function getStylePath() |
||
| 215 | { |
||
| 216 | $def = $this->getDefinition(); |
||
| 217 | if($def){ |
||
| 218 | return $def->getStylePath($this->app); |
||
| 219 | } |
||
| 220 | return null; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function getScriptPath() |
||
| 224 | { |
||
| 225 | $def = $this->getDefinition(); |
||
| 226 | if($def){ |
||
| 227 | return $def->getScriptPath($this->app); |
||
| 228 | } |
||
| 229 | return null; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @see Func_App::translate() |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function translate($str, $str_plurals = null, $number = null) |
||
| 237 | { |
||
| 238 | $currentComponent = $this->app->getCurrentComponent(); // Get actual currentComponent |
||
| 239 | $this->app->setCurrentComponent($this); // Set currentComponent to self. This is used in Func_App::translate() |
||
| 240 | $translation = $this->app->translate($str, $str_plurals, $number); |
||
| 241 | $this->app->setCurrentComponent($currentComponent); // Reset the currentComponent to what it was before |
||
| 242 | return $translation; |
||
| 243 | } |
||
| 244 | } |