@@ -30,54 +30,54 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | class Context |
| 32 | 32 | { |
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * @var Context|null |
| 35 | 35 | */ |
| 36 | - private $__extends__ = null; |
|
| 36 | +private $__extends__ = null; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * @var string |
| 40 | 40 | */ |
| 41 | - private $__block_name__; |
|
| 41 | +private $__block_name__; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * @var array |
| 45 | 45 | */ |
| 46 | - private $__properties__ = []; |
|
| 46 | +private $__properties__ = []; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * The constructor |
| 50 | 50 | * |
| 51 | 51 | * @param array $__namespaces__ |
| 52 | 52 | * @param string $__default_namespace__ |
| 53 | 53 | * @param string $__template__ |
| 54 | 54 | */ |
| 55 | - public function __construct(protected array $__namespaces__, |
|
| 56 | - protected string $__default_namespace__, protected string $__template__) |
|
| 57 | - {} |
|
| 55 | +public function __construct(protected array $__namespaces__, |
|
| 56 | +protected string $__default_namespace__, protected string $__template__) |
|
| 57 | +{} |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * @param string $name |
| 61 | 61 | * |
| 62 | 62 | * @return mixed |
| 63 | 63 | */ |
| 64 | - public function __get(string $name): mixed |
|
| 65 | - { |
|
| 66 | - return $this->__properties__[$name] ?? ''; |
|
| 67 | - } |
|
| 64 | +public function __get(string $name): mixed |
|
| 65 | +{ |
|
| 66 | +return $this->__properties__[$name] ?? ''; |
|
| 67 | +} |
|
| 68 | 68 | |
| 69 | - /** |
|
| 69 | +/** |
|
| 70 | 70 | * @param string $name |
| 71 | 71 | * @param mixed $value |
| 72 | 72 | * |
| 73 | 73 | * @return void |
| 74 | 74 | */ |
| 75 | - public function __set(string $name, $value): void |
|
| 76 | - { |
|
| 77 | - $this->__properties__[$name] = $value; |
|
| 78 | - } |
|
| 75 | +public function __set(string $name, $value): void |
|
| 76 | +{ |
|
| 77 | +$this->__properties__[$name] = $value; |
|
| 78 | +} |
|
| 79 | 79 | |
| 80 | - /** |
|
| 80 | +/** |
|
| 81 | 81 | * Include a template |
| 82 | 82 | * |
| 83 | 83 | * @param string $template The name of template to be rendered |
@@ -85,110 +85,110 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return void |
| 87 | 87 | */ |
| 88 | - protected function include(string $template, array $vars = []): void |
|
| 89 | - { |
|
| 90 | - $context = new Context($this->__namespaces__, |
|
| 91 | - $this->__default_namespace__, $template); |
|
| 92 | - echo $context->__render($vars); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 88 | +protected function include(string $template, array $vars = []): void |
|
| 89 | +{ |
|
| 90 | +$context = new Context($this->__namespaces__, |
|
| 91 | +$this->__default_namespace__, $template); |
|
| 92 | +echo $context->__render($vars); |
|
| 93 | +} |
|
| 94 | + |
|
| 95 | +/** |
|
| 96 | 96 | * @param string $template The name of template to be rendered |
| 97 | 97 | * |
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | - public function extends(string $template): void |
|
| 101 | - { |
|
| 102 | - $this->__extends__ = new Context($this->__namespaces__, |
|
| 103 | - $this->__default_namespace__, $template); |
|
| 104 | - } |
|
| 100 | +public function extends(string $template): void |
|
| 101 | +{ |
|
| 102 | +$this->__extends__ = new Context($this->__namespaces__, |
|
| 103 | +$this->__default_namespace__, $template); |
|
| 104 | +} |
|
| 105 | 105 | |
| 106 | - /** |
|
| 106 | +/** |
|
| 107 | 107 | * Start a new block |
| 108 | 108 | * |
| 109 | 109 | * @param string $name |
| 110 | 110 | * |
| 111 | 111 | * @return void |
| 112 | 112 | */ |
| 113 | - public function block(string $name): void |
|
| 114 | - { |
|
| 115 | - ob_start(); |
|
| 116 | - $this->__block_name__ = $name; |
|
| 117 | - } |
|
| 113 | +public function block(string $name): void |
|
| 114 | +{ |
|
| 115 | +ob_start(); |
|
| 116 | +$this->__block_name__ = $name; |
|
| 117 | +} |
|
| 118 | 118 | |
| 119 | - /** |
|
| 119 | +/** |
|
| 120 | 120 | * End the current block |
| 121 | 121 | * |
| 122 | 122 | * @param Closure|null $filter |
| 123 | 123 | * |
| 124 | 124 | * @return void |
| 125 | 125 | */ |
| 126 | - public function endblock(?Closure $filter = null): void |
|
| 127 | - { |
|
| 128 | - $content = ob_get_clean(); |
|
| 129 | - $this->__set($this->__block_name__, !$filter ? $content : $filter($content)); |
|
| 130 | - } |
|
| 126 | +public function endblock(?Closure $filter = null): void |
|
| 127 | +{ |
|
| 128 | +$content = ob_get_clean(); |
|
| 129 | +$this->__set($this->__block_name__, !$filter ? $content : $filter($content)); |
|
| 130 | +} |
|
| 131 | 131 | |
| 132 | - /** |
|
| 132 | +/** |
|
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | - private function __path(): string |
|
| 136 | - { |
|
| 137 | - $template = trim($this->__template__); |
|
| 138 | - // Get the namespace name |
|
| 139 | - $namespace = $this->__default_namespace__; |
|
| 140 | - $separatorPosition = strrpos($template, '::'); |
|
| 141 | - if($separatorPosition !== false) |
|
| 142 | - { |
|
| 143 | - $namespace = substr($template, 0, $separatorPosition); |
|
| 144 | - $template = substr($template, $separatorPosition + 2); |
|
| 145 | - } |
|
| 146 | - // Check if the namespace is defined |
|
| 147 | - if(!isset($this->__namespaces__[$namespace])) |
|
| 148 | - { |
|
| 149 | - return $template; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $namespace = $this->__namespaces__[$namespace]; |
|
| 153 | - // Get the template path |
|
| 154 | - return $namespace['directory'] . $template . $namespace['extension']; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 135 | +private function __path(): string |
|
| 136 | +{ |
|
| 137 | +$template = trim($this->__template__); |
|
| 138 | +// Get the namespace name |
|
| 139 | +$namespace = $this->__default_namespace__; |
|
| 140 | +$separatorPosition = strrpos($template, '::'); |
|
| 141 | +if($separatorPosition !== false) |
|
| 142 | +{ |
|
| 143 | +$namespace = substr($template, 0, $separatorPosition); |
|
| 144 | +$template = substr($template, $separatorPosition + 2); |
|
| 145 | +} |
|
| 146 | +// Check if the namespace is defined |
|
| 147 | +if(!isset($this->__namespaces__[$namespace])) |
|
| 148 | +{ |
|
| 149 | +return $template; |
|
| 150 | +} |
|
| 151 | + |
|
| 152 | +$namespace = $this->__namespaces__[$namespace]; |
|
| 153 | +// Get the template path |
|
| 154 | +return $namespace['directory'] . $template . $namespace['extension']; |
|
| 155 | +} |
|
| 156 | + |
|
| 157 | +/** |
|
| 158 | 158 | * Render a template |
| 159 | 159 | * |
| 160 | 160 | * @param array $vars The template vars |
| 161 | 161 | * |
| 162 | 162 | * @return string |
| 163 | 163 | */ |
| 164 | - public function __render(array $vars): string |
|
| 165 | - { |
|
| 166 | - // Get the template path |
|
| 167 | - $templatePath = $this->__path(); |
|
| 168 | - if(!@is_readable($templatePath)) |
|
| 169 | - { |
|
| 170 | - return ''; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Save the template properties. |
|
| 174 | - foreach($vars as $name => $value) |
|
| 175 | - { |
|
| 176 | - $this->__set((string)$name, $value); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Render the template |
|
| 180 | - $renderer = function() use($templatePath) { |
|
| 181 | - ob_start(); |
|
| 182 | - include $templatePath; |
|
| 183 | - $content = ob_get_clean(); |
|
| 184 | - |
|
| 185 | - return $this->__extends__ === null ? $content : |
|
| 186 | - // Render the extended template with the same properties. |
|
| 187 | - $this->__extends__->__render($this->__properties__); |
|
| 188 | - }; |
|
| 189 | - |
|
| 190 | - // Call the closure in the context of this object. |
|
| 191 | - // So the keyword '$this' in the template will refer to this object. |
|
| 192 | - return call_user_func($renderer->bindTo($this)); |
|
| 193 | - } |
|
| 164 | +public function __render(array $vars): string |
|
| 165 | +{ |
|
| 166 | +// Get the template path |
|
| 167 | +$templatePath = $this->__path(); |
|
| 168 | +if(!@is_readable($templatePath)) |
|
| 169 | +{ |
|
| 170 | +return ''; |
|
| 171 | +} |
|
| 172 | + |
|
| 173 | +// Save the template properties. |
|
| 174 | +foreach($vars as $name => $value) |
|
| 175 | +{ |
|
| 176 | +$this->__set((string)$name, $value); |
|
| 177 | +} |
|
| 178 | + |
|
| 179 | +// Render the template |
|
| 180 | +$renderer = function() use($templatePath) { |
|
| 181 | +ob_start(); |
|
| 182 | +include $templatePath; |
|
| 183 | +$content = ob_get_clean(); |
|
| 184 | + |
|
| 185 | +return $this->__extends__ === null ? $content : |
|
| 186 | + // Render the extended template with the same properties. |
|
| 187 | + $this->__extends__->__render($this->__properties__); |
|
| 188 | +}; |
|
| 189 | + |
|
| 190 | +// Call the closure in the context of this object. |
|
| 191 | +// So the keyword '$this' in the template will refer to this object. |
|
| 192 | +return call_user_func($renderer->bindTo($this)); |
|
| 193 | +} |
|
| 194 | 194 | } |
@@ -19,33 +19,33 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | class TemplateEngine |
| 21 | 21 | { |
| 22 | - /** |
|
| 22 | +/** |
|
| 23 | 23 | * The default namespace |
| 24 | 24 | * |
| 25 | 25 | * @var string |
| 26 | 26 | */ |
| 27 | - protected $sDefaultNamespace = ''; |
|
| 27 | +protected $sDefaultNamespace = ''; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * The namespaces |
| 31 | 31 | * |
| 32 | 32 | * @var array |
| 33 | 33 | */ |
| 34 | - protected $aNamespaces; |
|
| 34 | +protected $aNamespaces; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * Set the default namespace |
| 38 | 38 | * |
| 39 | 39 | * @param string $sDefaultNamespace |
| 40 | 40 | * |
| 41 | 41 | * @return void |
| 42 | 42 | */ |
| 43 | - public function setDefaultNamespace(string $sDefaultNamespace): void |
|
| 44 | - { |
|
| 45 | - $this->sDefaultNamespace = $sDefaultNamespace; |
|
| 46 | - } |
|
| 43 | +public function setDefaultNamespace(string $sDefaultNamespace): void |
|
| 44 | +{ |
|
| 45 | +$this->sDefaultNamespace = $sDefaultNamespace; |
|
| 46 | +} |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * Add a namespace to the template system |
| 50 | 50 | * |
| 51 | 51 | * @param string $sNamespace The namespace name |
@@ -54,16 +54,16 @@ discard block |
||
| 54 | 54 | * |
| 55 | 55 | * @return static |
| 56 | 56 | */ |
| 57 | - public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): static |
|
| 58 | - { |
|
| 59 | - $this->aNamespaces[$sNamespace] = [ |
|
| 60 | - 'directory' => rtrim(trim($sDirectory), "/\\") . DIRECTORY_SEPARATOR, |
|
| 61 | - 'extension' => $sExtension, |
|
| 62 | - ]; |
|
| 63 | - return $this; |
|
| 64 | - } |
|
| 57 | +public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): static |
|
| 58 | +{ |
|
| 59 | +$this->aNamespaces[$sNamespace] = [ |
|
| 60 | +'directory' => rtrim(trim($sDirectory), "/\\") . DIRECTORY_SEPARATOR, |
|
| 61 | +'extension' => $sExtension, |
|
| 62 | +]; |
|
| 63 | +return $this; |
|
| 64 | +} |
|
| 65 | 65 | |
| 66 | - /** |
|
| 66 | +/** |
|
| 67 | 67 | * Render a template |
| 68 | 68 | * |
| 69 | 69 | * @param string $sTemplate The name of template to be rendered |
@@ -71,9 +71,9 @@ discard block |
||
| 71 | 71 | * |
| 72 | 72 | * @return string |
| 73 | 73 | */ |
| 74 | - public function render(string $sTemplate, array $aVars = []): string |
|
| 75 | - { |
|
| 76 | - $context = new Context($this->aNamespaces, $this->sDefaultNamespace, $sTemplate); |
|
| 77 | - return $context->__render($aVars); |
|
| 78 | - } |
|
| 74 | +public function render(string $sTemplate, array $aVars = []): string |
|
| 75 | +{ |
|
| 76 | +$context = new Context($this->aNamespaces, $this->sDefaultNamespace, $sTemplate); |
|
| 77 | +return $context->__render($aVars); |
|
| 78 | +} |
|
| 79 | 79 | } |
@@ -26,43 +26,43 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | class Translator |
| 28 | 28 | { |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * The default locale |
| 31 | 31 | * |
| 32 | 32 | * @var string |
| 33 | 33 | */ |
| 34 | - protected $sDefaultLocale = 'en'; |
|
| 34 | +protected $sDefaultLocale = 'en'; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * The translations in a flattened array |
| 38 | 38 | * |
| 39 | 39 | * @var array |
| 40 | 40 | */ |
| 41 | - protected $aTranslations = []; |
|
| 41 | +protected $aTranslations = []; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * The translations as received as input |
| 45 | 45 | * |
| 46 | 46 | * @var array |
| 47 | 47 | */ |
| 48 | - protected $aRawTranslations = []; |
|
| 48 | +protected $aRawTranslations = []; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Set the default locale |
| 52 | 52 | * |
| 53 | 53 | * @param string $sLocale |
| 54 | 54 | * |
| 55 | 55 | * @return void |
| 56 | 56 | */ |
| 57 | - public function setLocale(string $sLocale): void |
|
| 58 | - { |
|
| 59 | - if(($sLocale = trim($sLocale))) |
|
| 60 | - { |
|
| 61 | - $this->sDefaultLocale = $sLocale; |
|
| 62 | - } |
|
| 63 | - } |
|
| 57 | +public function setLocale(string $sLocale): void |
|
| 58 | +{ |
|
| 59 | +if(($sLocale = trim($sLocale))) |
|
| 60 | +{ |
|
| 61 | +$this->sDefaultLocale = $sLocale; |
|
| 62 | +} |
|
| 63 | +} |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * Recursively load translated strings from an array |
| 67 | 67 | * |
| 68 | 68 | * @param string $sLanguage The language of the translations |
@@ -71,26 +71,26 @@ discard block |
||
| 71 | 71 | * |
| 72 | 72 | * @return void |
| 73 | 73 | */ |
| 74 | - private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
|
| 75 | - { |
|
| 76 | - foreach($aTranslations as $sKey => $xTranslation) |
|
| 77 | - { |
|
| 78 | - $sKey = trim($sKey); |
|
| 79 | - $sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
|
| 80 | - if(is_array($xTranslation)) |
|
| 81 | - { |
|
| 82 | - // Recursively read the translations in the array |
|
| 83 | - $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
|
| 84 | - } |
|
| 85 | - else |
|
| 86 | - { |
|
| 87 | - // Save this translation |
|
| 88 | - $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 74 | +private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
|
| 75 | +{ |
|
| 76 | +foreach($aTranslations as $sKey => $xTranslation) |
|
| 77 | +{ |
|
| 78 | +$sKey = trim($sKey); |
|
| 79 | +$sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
|
| 80 | +if(is_array($xTranslation)) |
|
| 81 | +{ |
|
| 82 | + // Recursively read the translations in the array |
|
| 83 | + $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
|
| 84 | +} |
|
| 85 | +else |
|
| 86 | +{ |
|
| 87 | + // Save this translation |
|
| 88 | + $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
|
| 89 | +} |
|
| 90 | +} |
|
| 91 | +} |
|
| 92 | 92 | |
| 93 | - /** |
|
| 93 | +/** |
|
| 94 | 94 | * Load translated strings from a file |
| 95 | 95 | * |
| 96 | 96 | * @param string $sFilePath The file full path |
@@ -98,30 +98,30 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return bool |
| 100 | 100 | */ |
| 101 | - public function loadTranslations(string $sFilePath, string $sLanguage): bool |
|
| 102 | - { |
|
| 103 | - if(!file_exists($sFilePath)) |
|
| 104 | - { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - $aTranslations = require($sFilePath); |
|
| 108 | - if(!is_array($aTranslations)) |
|
| 109 | - { |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 101 | +public function loadTranslations(string $sFilePath, string $sLanguage): bool |
|
| 102 | +{ |
|
| 103 | +if(!file_exists($sFilePath)) |
|
| 104 | +{ |
|
| 105 | +return false; |
|
| 106 | +} |
|
| 107 | +$aTranslations = require($sFilePath); |
|
| 108 | +if(!is_array($aTranslations)) |
|
| 109 | +{ |
|
| 110 | +return false; |
|
| 111 | +} |
|
| 112 | 112 | |
| 113 | - // Load the translations |
|
| 114 | - if(!isset($this->aTranslations[$sLanguage])) |
|
| 115 | - { |
|
| 116 | - $this->aTranslations[$sLanguage] = []; |
|
| 117 | - } |
|
| 118 | - $this->aRawTranslations[$sLanguage] = |
|
| 119 | - array_merge($this->aRawTranslations[$sLanguage] ?? [], $aTranslations); |
|
| 120 | - $this->_loadTranslations($sLanguage, '', $aTranslations); |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 113 | +// Load the translations |
|
| 114 | +if(!isset($this->aTranslations[$sLanguage])) |
|
| 115 | +{ |
|
| 116 | +$this->aTranslations[$sLanguage] = []; |
|
| 117 | +} |
|
| 118 | +$this->aRawTranslations[$sLanguage] = |
|
| 119 | +array_merge($this->aRawTranslations[$sLanguage] ?? [], $aTranslations); |
|
| 120 | +$this->_loadTranslations($sLanguage, '', $aTranslations); |
|
| 121 | +return true; |
|
| 122 | +} |
|
| 123 | 123 | |
| 124 | - /** |
|
| 124 | +/** |
|
| 125 | 125 | * Get a translated string |
| 126 | 126 | * |
| 127 | 127 | * @param string $sText The key of the translated string |
@@ -130,29 +130,29 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | - public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
|
| 134 | - { |
|
| 135 | - $sText = trim($sText); |
|
| 136 | - if(empty($sLanguage)) |
|
| 137 | - { |
|
| 138 | - $sLanguage = $this->sDefaultLocale; |
|
| 139 | - } |
|
| 140 | - if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | - { |
|
| 142 | - return $sText; |
|
| 143 | - } |
|
| 144 | - $sMessage = $this->aTranslations[$sLanguage][$sText]; |
|
| 145 | - if(!empty($aPlaceHolders)) |
|
| 146 | - { |
|
| 147 | - $aVars = array_map(function($sVar) { |
|
| 148 | - return ':' . $sVar; |
|
| 149 | - }, array_keys($aPlaceHolders)); |
|
| 150 | - $sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage); |
|
| 151 | - } |
|
| 152 | - return $sMessage; |
|
| 153 | - } |
|
| 133 | +public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
|
| 134 | +{ |
|
| 135 | +$sText = trim($sText); |
|
| 136 | +if(empty($sLanguage)) |
|
| 137 | +{ |
|
| 138 | +$sLanguage = $this->sDefaultLocale; |
|
| 139 | +} |
|
| 140 | +if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | +{ |
|
| 142 | +return $sText; |
|
| 143 | +} |
|
| 144 | +$sMessage = $this->aTranslations[$sLanguage][$sText]; |
|
| 145 | +if(!empty($aPlaceHolders)) |
|
| 146 | +{ |
|
| 147 | +$aVars = array_map(function($sVar) { |
|
| 148 | + return ':' . $sVar; |
|
| 149 | +}, array_keys($aPlaceHolders)); |
|
| 150 | +$sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage); |
|
| 151 | +} |
|
| 152 | +return $sMessage; |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * Get all the translations under a given key |
| 157 | 157 | * |
| 158 | 158 | * @param string $sKey |
@@ -160,22 +160,22 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @return array |
| 162 | 162 | */ |
| 163 | - public function translations(string $sKey, string $sLanguage = ''): array |
|
| 164 | - { |
|
| 165 | - if(empty($sLanguage)) |
|
| 166 | - { |
|
| 167 | - $sLanguage = $this->sDefaultLocale; |
|
| 168 | - } |
|
| 169 | - $aKeys = explode('.', $sKey); |
|
| 163 | +public function translations(string $sKey, string $sLanguage = ''): array |
|
| 164 | +{ |
|
| 165 | +if(empty($sLanguage)) |
|
| 166 | +{ |
|
| 167 | +$sLanguage = $this->sDefaultLocale; |
|
| 168 | +} |
|
| 169 | +$aKeys = explode('.', $sKey); |
|
| 170 | 170 | |
| 171 | - $aTranslations = $this->aRawTranslations[$sLanguage]; |
|
| 172 | - foreach($aKeys as $sKey) |
|
| 173 | - { |
|
| 174 | - if($sKey !== '') |
|
| 175 | - { |
|
| 176 | - $aTranslations = $aTranslations[$sKey] ?? []; |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - return $aTranslations; |
|
| 180 | - } |
|
| 171 | +$aTranslations = $this->aRawTranslations[$sLanguage]; |
|
| 172 | +foreach($aKeys as $sKey) |
|
| 173 | +{ |
|
| 174 | +if($sKey !== '') |
|
| 175 | +{ |
|
| 176 | + $aTranslations = $aTranslations[$sKey] ?? []; |
|
| 177 | +} |
|
| 178 | +} |
|
| 179 | +return $aTranslations; |
|
| 180 | +} |
|
| 181 | 181 | } |
@@ -16,27 +16,27 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | function _register(): void |
| 18 | 18 | { |
| 19 | - $di = jaxon()->di(); |
|
| 19 | +$di = jaxon()->di(); |
|
| 20 | 20 | |
| 21 | - $di->set(AnnotationReader::class, function($c) { |
|
| 22 | - $sKey = 'jaxon_annotations_cache_dir'; |
|
| 23 | - $sCacheDir = $c->h($sKey) ? $c->g($sKey) : sys_get_temp_dir(); |
|
| 24 | - $xAnnotationManager = new AnnotationManager(); |
|
| 25 | - $xAnnotationManager->cache = new AnnotationCache($sCacheDir); |
|
| 21 | +$di->set(AnnotationReader::class, function($c) { |
|
| 22 | +$sKey = 'jaxon_annotations_cache_dir'; |
|
| 23 | +$sCacheDir = $c->h($sKey) ? $c->g($sKey) : sys_get_temp_dir(); |
|
| 24 | +$xAnnotationManager = new AnnotationManager(); |
|
| 25 | +$xAnnotationManager->cache = new AnnotationCache($sCacheDir); |
|
| 26 | 26 | |
| 27 | - return new AnnotationReader($xAnnotationManager); |
|
| 28 | - }); |
|
| 27 | +return new AnnotationReader($xAnnotationManager); |
|
| 28 | +}); |
|
| 29 | 29 | |
| 30 | - $di->alias('metadata_reader_annotations', AnnotationReader::class); |
|
| 30 | +$di->alias('metadata_reader_annotations', AnnotationReader::class); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | function register(): void |
| 34 | 34 | { |
| 35 | - // Do nothing if running in cli. |
|
| 36 | - if(php_sapi_name() !== 'cli') |
|
| 37 | - { |
|
| 38 | - _register(); |
|
| 39 | - }; |
|
| 35 | +// Do nothing if running in cli. |
|
| 36 | +if(php_sapi_name() !== 'cli') |
|
| 37 | +{ |
|
| 38 | +_register(); |
|
| 39 | +}; |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | register(); |
@@ -18,22 +18,22 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | class TemplateView implements ViewInterface |
| 20 | 20 | { |
| 21 | - /** |
|
| 21 | +/** |
|
| 22 | 22 | * The Jaxon template engine |
| 23 | 23 | * |
| 24 | 24 | * @var TemplateEngine |
| 25 | 25 | */ |
| 26 | - protected $xTemplateEngine; |
|
| 26 | +protected $xTemplateEngine; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 28 | +/** |
|
| 29 | 29 | * The class constructor |
| 30 | 30 | */ |
| 31 | - public function __construct(TemplateEngine $xTemplateEngine) |
|
| 32 | - { |
|
| 33 | - $this->xTemplateEngine = $xTemplateEngine; |
|
| 34 | - } |
|
| 31 | +public function __construct(TemplateEngine $xTemplateEngine) |
|
| 32 | +{ |
|
| 33 | +$this->xTemplateEngine = $xTemplateEngine; |
|
| 34 | +} |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * Add a namespace to this view renderer |
| 38 | 38 | * |
| 39 | 39 | * @param string $sNamespace The namespace name |
@@ -42,28 +42,28 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @return void |
| 44 | 44 | */ |
| 45 | - public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): void |
|
| 46 | - { |
|
| 47 | - $this->xTemplateEngine->addNamespace($sNamespace, $sDirectory, $sExtension); |
|
| 48 | - } |
|
| 45 | +public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = ''): void |
|
| 46 | +{ |
|
| 47 | +$this->xTemplateEngine->addNamespace($sNamespace, $sDirectory, $sExtension); |
|
| 48 | +} |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Render a view |
| 52 | 52 | * |
| 53 | 53 | * @param Store $store A store populated with the view data |
| 54 | 54 | * |
| 55 | 55 | * @return string The string representation of the view |
| 56 | 56 | */ |
| 57 | - public function render(Store $store): string |
|
| 58 | - { |
|
| 59 | - $sViewName = $store->getViewName(); |
|
| 60 | - $sNamespace = $store->getNamespace(); |
|
| 61 | - // In this view renderer, the namespace must always be prepended to the view name. |
|
| 62 | - if(substr($sViewName, 0, strlen($sNamespace) + 2) != $sNamespace . '::') |
|
| 63 | - { |
|
| 64 | - $sViewName = $sNamespace . '::' . $sViewName; |
|
| 65 | - } |
|
| 66 | - // Render the template |
|
| 67 | - return trim($this->xTemplateEngine->render($sViewName, $store->getViewData()), " \t\n"); |
|
| 68 | - } |
|
| 57 | +public function render(Store $store): string |
|
| 58 | +{ |
|
| 59 | +$sViewName = $store->getViewName(); |
|
| 60 | +$sNamespace = $store->getNamespace(); |
|
| 61 | +// In this view renderer, the namespace must always be prepended to the view name. |
|
| 62 | +if(substr($sViewName, 0, strlen($sNamespace) + 2) != $sNamespace . '::') |
|
| 63 | +{ |
|
| 64 | +$sViewName = $sNamespace . '::' . $sViewName; |
|
| 65 | +} |
|
| 66 | +// Render the template |
|
| 67 | +return trim($this->xTemplateEngine->render($sViewName, $store->getViewData()), " \t\n"); |
|
| 68 | +} |
|
| 69 | 69 | } |
@@ -20,50 +20,50 @@ |
||
| 20 | 20 | |
| 21 | 21 | class InputData |
| 22 | 22 | { |
| 23 | - /** |
|
| 23 | +/** |
|
| 24 | 24 | * @var ReflectionClass |
| 25 | 25 | */ |
| 26 | - private $xReflectionClass; |
|
| 26 | +private $xReflectionClass; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 28 | +/** |
|
| 29 | 29 | * @param ReflectionClass|string $xClass |
| 30 | 30 | * @param array $aMethods |
| 31 | 31 | * @param array $aProperties |
| 32 | 32 | */ |
| 33 | - public function __construct(ReflectionClass|string $xClass, |
|
| 34 | - private array $aMethods = [], private array $aProperties = []) |
|
| 35 | - { |
|
| 36 | - $this->xReflectionClass = is_string($xClass) ? |
|
| 37 | - new ReflectionClass($xClass) : $xClass; |
|
| 38 | - } |
|
| 33 | +public function __construct(ReflectionClass|string $xClass, |
|
| 34 | +private array $aMethods = [], private array $aProperties = []) |
|
| 35 | +{ |
|
| 36 | +$this->xReflectionClass = is_string($xClass) ? |
|
| 37 | +new ReflectionClass($xClass) : $xClass; |
|
| 38 | +} |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * Get the reflection class |
| 42 | 42 | * |
| 43 | 43 | * @return ReflectionClass |
| 44 | 44 | */ |
| 45 | - public function getReflectionClass(): ReflectionClass |
|
| 46 | - { |
|
| 47 | - return $this->xReflectionClass; |
|
| 48 | - } |
|
| 45 | +public function getReflectionClass(): ReflectionClass |
|
| 46 | +{ |
|
| 47 | +return $this->xReflectionClass; |
|
| 48 | +} |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * The methods to check for metadata |
| 52 | 52 | * |
| 53 | 53 | * @return array |
| 54 | 54 | */ |
| 55 | - public function getMethods(): array |
|
| 56 | - { |
|
| 57 | - return $this->aMethods; |
|
| 58 | - } |
|
| 55 | +public function getMethods(): array |
|
| 56 | +{ |
|
| 57 | +return $this->aMethods; |
|
| 58 | +} |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * The properties to check for metadata |
| 62 | 62 | * |
| 63 | 63 | * @return array |
| 64 | 64 | */ |
| 65 | - public function getProperties(): array |
|
| 66 | - { |
|
| 67 | - return $this->aProperties; |
|
| 68 | - } |
|
| 65 | +public function getProperties(): array |
|
| 66 | +{ |
|
| 67 | +return $this->aProperties; |
|
| 68 | +} |
|
| 69 | 69 | } |
@@ -16,42 +16,42 @@ |
||
| 16 | 16 | |
| 17 | 17 | class ExcludeData extends AbstractData |
| 18 | 18 | { |
| 19 | - /** |
|
| 19 | +/** |
|
| 20 | 20 | * @var bool |
| 21 | 21 | */ |
| 22 | - private bool $bValue = true; |
|
| 22 | +private bool $bValue = true; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 24 | +/** |
|
| 25 | 25 | * @return string |
| 26 | 26 | */ |
| 27 | - public function getName(): string |
|
| 28 | - { |
|
| 29 | - return 'protected'; |
|
| 30 | - } |
|
| 27 | +public function getName(): string |
|
| 28 | +{ |
|
| 29 | +return 'protected'; |
|
| 30 | +} |
|
| 31 | 31 | |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * @return mixed |
| 34 | 34 | */ |
| 35 | - public function getValue(): mixed |
|
| 36 | - { |
|
| 37 | - return $this->bValue; |
|
| 38 | - } |
|
| 35 | +public function getValue(): mixed |
|
| 36 | +{ |
|
| 37 | +return $this->bValue; |
|
| 38 | +} |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * @param bool $bValue |
| 42 | 42 | * |
| 43 | 43 | * @return void |
| 44 | 44 | */ |
| 45 | - public function setValue(bool $bValue): void |
|
| 46 | - { |
|
| 47 | - $this->bValue = $bValue; |
|
| 48 | - } |
|
| 45 | +public function setValue(bool $bValue): void |
|
| 46 | +{ |
|
| 47 | +$this->bValue = $bValue; |
|
| 48 | +} |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * @inheritDoc |
| 52 | 52 | */ |
| 53 | - public function encode(string $sVarName): array |
|
| 54 | - { |
|
| 55 | - return ["{$sVarName}->setValue(" . ($this->bValue ? 'true' : 'false') . ");"]; |
|
| 56 | - } |
|
| 53 | +public function encode(string $sVarName): array |
|
| 54 | +{ |
|
| 55 | +return ["{$sVarName}->setValue(" . ($this->bValue ? 'true' : 'false') . ");"]; |
|
| 56 | +} |
|
| 57 | 57 | } |
@@ -20,61 +20,61 @@ |
||
| 20 | 20 | |
| 21 | 21 | class UploadData extends AbstractData |
| 22 | 22 | { |
| 23 | - /** |
|
| 23 | +/** |
|
| 24 | 24 | * The id of the upload field |
| 25 | 25 | * |
| 26 | 26 | * @var string |
| 27 | 27 | */ |
| 28 | - protected $sField = ''; |
|
| 28 | +protected $sField = ''; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 30 | +/** |
|
| 31 | 31 | * @return string |
| 32 | 32 | */ |
| 33 | - public function getName(): string |
|
| 34 | - { |
|
| 35 | - return 'upload'; |
|
| 36 | - } |
|
| 33 | +public function getName(): string |
|
| 34 | +{ |
|
| 35 | +return 'upload'; |
|
| 36 | +} |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * @return mixed |
| 40 | 40 | */ |
| 41 | - public function getValue(): mixed |
|
| 42 | - { |
|
| 43 | - // The field id is surrounded with simple quotes. |
|
| 44 | - return "'{$this->sField}'"; |
|
| 45 | - } |
|
| 41 | +public function getValue(): mixed |
|
| 42 | +{ |
|
| 43 | +// The field id is surrounded with simple quotes. |
|
| 44 | +return "'{$this->sField}'"; |
|
| 45 | +} |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * @param string $sField |
| 49 | 49 | * |
| 50 | 50 | * @return void |
| 51 | 51 | */ |
| 52 | - protected function validateField(string $sField): void |
|
| 53 | - { |
|
| 54 | - if(preg_match('/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/', $sField) > 0) |
|
| 55 | - { |
|
| 56 | - return; |
|
| 57 | - } |
|
| 58 | - throw new SetupException("$sField is not a valid \"field\" value for upload"); |
|
| 59 | - } |
|
| 52 | +protected function validateField(string $sField): void |
|
| 53 | +{ |
|
| 54 | +if(preg_match('/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/', $sField) > 0) |
|
| 55 | +{ |
|
| 56 | +return; |
|
| 57 | +} |
|
| 58 | +throw new SetupException("$sField is not a valid \"field\" value for upload"); |
|
| 59 | +} |
|
| 60 | 60 | |
| 61 | - /** |
|
| 61 | +/** |
|
| 62 | 62 | * @param string $sField |
| 63 | 63 | * |
| 64 | 64 | * @return void |
| 65 | 65 | */ |
| 66 | - public function setValue(string $sField): void |
|
| 67 | - { |
|
| 68 | - $this->validateField($sField); |
|
| 66 | +public function setValue(string $sField): void |
|
| 67 | +{ |
|
| 68 | +$this->validateField($sField); |
|
| 69 | 69 | |
| 70 | - $this->sField = $sField; |
|
| 71 | - } |
|
| 70 | +$this->sField = $sField; |
|
| 71 | +} |
|
| 72 | 72 | |
| 73 | - /** |
|
| 73 | +/** |
|
| 74 | 74 | * @inheritDoc |
| 75 | 75 | */ |
| 76 | - public function encode(string $sVarName): array |
|
| 77 | - { |
|
| 78 | - return ["{$sVarName}->setValue('{$this->sField}');"]; |
|
| 79 | - } |
|
| 76 | +public function encode(string $sVarName): array |
|
| 77 | +{ |
|
| 78 | +return ["{$sVarName}->setValue('{$this->sField}');"]; |
|
| 79 | +} |
|
| 80 | 80 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | class PackageManager |
| 36 | 36 | { |
| 37 | - /** |
|
| 37 | +/** |
|
| 38 | 38 | * The constructor |
| 39 | 39 | * |
| 40 | 40 | * @param Container $di |
@@ -46,48 +46,48 @@ discard block |
||
| 46 | 46 | * @param CallbackManager $xCallbackManager |
| 47 | 47 | * @param ComponentRegistry $xRegistry |
| 48 | 48 | */ |
| 49 | - public function __construct(private Container $di, private Translator $xTranslator, |
|
| 50 | - private PluginManager $xPluginManager, private ConfigManager $xConfigManager, |
|
| 51 | - private CodeGenerator $xCodeGenerator, private ViewRenderer $xViewRenderer, |
|
| 52 | - private CallbackManager $xCallbackManager, private ComponentRegistry $xRegistry) |
|
| 53 | - {} |
|
| 49 | +public function __construct(private Container $di, private Translator $xTranslator, |
|
| 50 | +private PluginManager $xPluginManager, private ConfigManager $xConfigManager, |
|
| 51 | +private CodeGenerator $xCodeGenerator, private ViewRenderer $xViewRenderer, |
|
| 52 | +private CallbackManager $xCallbackManager, private ComponentRegistry $xRegistry) |
|
| 53 | +{} |
|
| 54 | 54 | |
| 55 | - /** |
|
| 55 | +/** |
|
| 56 | 56 | * Save items in the DI container |
| 57 | 57 | * |
| 58 | 58 | * @param Config $xConfig |
| 59 | 59 | * |
| 60 | 60 | * @return void |
| 61 | 61 | */ |
| 62 | - private function updateContainer(Config $xConfig): void |
|
| 63 | - { |
|
| 64 | - $aOptions = $xConfig->getOption('container.set', []); |
|
| 65 | - foreach($aOptions as $xKey => $xValue) |
|
| 66 | - { |
|
| 67 | - // The key is the class name. It must be a string. |
|
| 68 | - $this->di->set((string)$xKey, $xValue); |
|
| 69 | - } |
|
| 70 | - $aOptions = $xConfig->getOption('container.val', []); |
|
| 71 | - foreach($aOptions as $xKey => $xValue) |
|
| 72 | - { |
|
| 73 | - // The key is the class name. It must be a string. |
|
| 74 | - $this->di->val((string)$xKey, $xValue); |
|
| 75 | - } |
|
| 76 | - $aOptions = $xConfig->getOption('container.auto', []); |
|
| 77 | - foreach($aOptions as $xValue) |
|
| 78 | - { |
|
| 79 | - // The key is the class name. It must be a string. |
|
| 80 | - $this->di->auto((string)$xValue); |
|
| 81 | - } |
|
| 82 | - $aOptions = $xConfig->getOption('container.alias', []); |
|
| 83 | - foreach($aOptions as $xKey => $xValue) |
|
| 84 | - { |
|
| 85 | - // The key is the class name. It must be a string. |
|
| 86 | - $this->di->alias((string)$xKey, (string)$xValue); |
|
| 87 | - } |
|
| 88 | - } |
|
| 62 | +private function updateContainer(Config $xConfig): void |
|
| 63 | +{ |
|
| 64 | +$aOptions = $xConfig->getOption('container.set', []); |
|
| 65 | +foreach($aOptions as $xKey => $xValue) |
|
| 66 | +{ |
|
| 67 | +// The key is the class name. It must be a string. |
|
| 68 | +$this->di->set((string)$xKey, $xValue); |
|
| 69 | +} |
|
| 70 | +$aOptions = $xConfig->getOption('container.val', []); |
|
| 71 | +foreach($aOptions as $xKey => $xValue) |
|
| 72 | +{ |
|
| 73 | +// The key is the class name. It must be a string. |
|
| 74 | +$this->di->val((string)$xKey, $xValue); |
|
| 75 | +} |
|
| 76 | +$aOptions = $xConfig->getOption('container.auto', []); |
|
| 77 | +foreach($aOptions as $xValue) |
|
| 78 | +{ |
|
| 79 | +// The key is the class name. It must be a string. |
|
| 80 | +$this->di->auto((string)$xValue); |
|
| 81 | +} |
|
| 82 | +$aOptions = $xConfig->getOption('container.alias', []); |
|
| 83 | +foreach($aOptions as $xKey => $xValue) |
|
| 84 | +{ |
|
| 85 | +// The key is the class name. It must be a string. |
|
| 86 | +$this->di->alias((string)$xKey, (string)$xValue); |
|
| 87 | +} |
|
| 88 | +} |
|
| 89 | 89 | |
| 90 | - /** |
|
| 90 | +/** |
|
| 91 | 91 | * Register callables from a section of the config |
| 92 | 92 | * |
| 93 | 93 | * @param array $aOptions The content of the config section |
@@ -96,41 +96,41 @@ discard block |
||
| 96 | 96 | * @return void |
| 97 | 97 | * @throws SetupException |
| 98 | 98 | */ |
| 99 | - private function registerCallables(array $aOptions, string $sCallableType): void |
|
| 100 | - { |
|
| 101 | - foreach($aOptions as $xKey => $xValue) |
|
| 102 | - { |
|
| 103 | - if(is_integer($xKey) && is_string($xValue)) |
|
| 104 | - { |
|
| 105 | - // Register a function without options |
|
| 106 | - $this->xPluginManager->registerCallable($sCallableType, $xValue); |
|
| 107 | - continue; |
|
| 108 | - } |
|
| 99 | +private function registerCallables(array $aOptions, string $sCallableType): void |
|
| 100 | +{ |
|
| 101 | +foreach($aOptions as $xKey => $xValue) |
|
| 102 | +{ |
|
| 103 | +if(is_integer($xKey) && is_string($xValue)) |
|
| 104 | +{ |
|
| 105 | + // Register a function without options |
|
| 106 | + $this->xPluginManager->registerCallable($sCallableType, $xValue); |
|
| 107 | + continue; |
|
| 108 | +} |
|
| 109 | 109 | |
| 110 | - if(is_string($xKey) && (is_array($xValue) || is_string($xValue))) |
|
| 111 | - { |
|
| 112 | - // Register a function with options |
|
| 113 | - $this->xPluginManager->registerCallable($sCallableType, $xKey, $xValue); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - } |
|
| 110 | +if(is_string($xKey) && (is_array($xValue) || is_string($xValue))) |
|
| 111 | +{ |
|
| 112 | + // Register a function with options |
|
| 113 | + $this->xPluginManager->registerCallable($sCallableType, $xKey, $xValue); |
|
| 114 | +} |
|
| 115 | +} |
|
| 116 | +} |
|
| 117 | 117 | |
| 118 | - /** |
|
| 118 | +/** |
|
| 119 | 119 | * Register exceptions handlers |
| 120 | 120 | * |
| 121 | 121 | * @param Config $xConfig |
| 122 | 122 | * |
| 123 | 123 | * @return void |
| 124 | 124 | */ |
| 125 | - private function registerExceptionHandlers(Config $xConfig): void |
|
| 126 | - { |
|
| 127 | - foreach($xConfig->getOption('exceptions', []) as $sExClass => $xExHandler) |
|
| 128 | - { |
|
| 129 | - $this->xCallbackManager->error($xExHandler, is_string($sExClass) ? $sExClass : ''); |
|
| 130 | - } |
|
| 131 | - } |
|
| 125 | +private function registerExceptionHandlers(Config $xConfig): void |
|
| 126 | +{ |
|
| 127 | +foreach($xConfig->getOption('exceptions', []) as $sExClass => $xExHandler) |
|
| 128 | +{ |
|
| 129 | +$this->xCallbackManager->error($xExHandler, is_string($sExClass) ? $sExClass : ''); |
|
| 130 | +} |
|
| 131 | +} |
|
| 132 | 132 | |
| 133 | - /** |
|
| 133 | +/** |
|
| 134 | 134 | * Get a callable list from config |
| 135 | 135 | * |
| 136 | 136 | * @param Config $xConfig |
@@ -140,36 +140,36 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @return void |
| 142 | 142 | */ |
| 143 | - private function registerCallablesFromConfig(Config $xConfig, |
|
| 144 | - string $sOptionName, string $sOptionKey, string $sCallableType): void |
|
| 145 | - { |
|
| 146 | - // The callable (directory path, class or function name) can be used as the |
|
| 147 | - // key of the array item, a string as the value of an entry without a key, |
|
| 148 | - // or set with the key $sOptionKey in an array entry without a key. |
|
| 149 | - $aCallables = []; |
|
| 150 | - foreach($xConfig->getOption($sOptionName, []) as $xKey => $xValue) |
|
| 151 | - { |
|
| 152 | - if(is_string($xKey)) |
|
| 153 | - { |
|
| 154 | - $aCallables[$xKey] = $xValue; |
|
| 155 | - continue; |
|
| 156 | - } |
|
| 157 | - if(is_string($xValue)) |
|
| 158 | - { |
|
| 159 | - $aCallables[] = $xValue; |
|
| 160 | - continue; |
|
| 161 | - } |
|
| 143 | +private function registerCallablesFromConfig(Config $xConfig, |
|
| 144 | +string $sOptionName, string $sOptionKey, string $sCallableType): void |
|
| 145 | +{ |
|
| 146 | +// The callable (directory path, class or function name) can be used as the |
|
| 147 | +// key of the array item, a string as the value of an entry without a key, |
|
| 148 | +// or set with the key $sOptionKey in an array entry without a key. |
|
| 149 | +$aCallables = []; |
|
| 150 | +foreach($xConfig->getOption($sOptionName, []) as $xKey => $xValue) |
|
| 151 | +{ |
|
| 152 | +if(is_string($xKey)) |
|
| 153 | +{ |
|
| 154 | + $aCallables[$xKey] = $xValue; |
|
| 155 | + continue; |
|
| 156 | +} |
|
| 157 | +if(is_string($xValue)) |
|
| 158 | +{ |
|
| 159 | + $aCallables[] = $xValue; |
|
| 160 | + continue; |
|
| 161 | +} |
|
| 162 | 162 | |
| 163 | - if(is_array($xValue) && isset($xValue[$sOptionKey])) |
|
| 164 | - { |
|
| 165 | - $aCallables[$xValue[$sOptionKey]] = $xValue; |
|
| 166 | - } |
|
| 167 | - // Invalid values are ignored. |
|
| 168 | - } |
|
| 169 | - $this->registerCallables($aCallables, $sCallableType); |
|
| 170 | - } |
|
| 163 | +if(is_array($xValue) && isset($xValue[$sOptionKey])) |
|
| 164 | +{ |
|
| 165 | + $aCallables[$xValue[$sOptionKey]] = $xValue; |
|
| 166 | +} |
|
| 167 | +// Invalid values are ignored. |
|
| 168 | +} |
|
| 169 | +$this->registerCallables($aCallables, $sCallableType); |
|
| 170 | +} |
|
| 171 | 171 | |
| 172 | - /** |
|
| 172 | +/** |
|
| 173 | 173 | * Read and set Jaxon options from a JSON config file |
| 174 | 174 | * |
| 175 | 175 | * @param Config $xConfig The config options |
@@ -177,33 +177,33 @@ discard block |
||
| 177 | 177 | * @return void |
| 178 | 178 | * @throws SetupException |
| 179 | 179 | */ |
| 180 | - private function registerItemsFromConfig(Config $xConfig): void |
|
| 181 | - { |
|
| 182 | - // Set the config for the registered callables. |
|
| 183 | - $this->xRegistry->setPackageConfig($xConfig); |
|
| 180 | +private function registerItemsFromConfig(Config $xConfig): void |
|
| 181 | +{ |
|
| 182 | +// Set the config for the registered callables. |
|
| 183 | +$this->xRegistry->setPackageConfig($xConfig); |
|
| 184 | 184 | |
| 185 | - // Register functions, classes and directories |
|
| 186 | - $this->registerCallablesFromConfig($xConfig, |
|
| 187 | - 'functions', 'name', Jaxon::CALLABLE_FUNCTION); |
|
| 188 | - $this->registerCallablesFromConfig($xConfig, |
|
| 189 | - 'classes', 'name', Jaxon::CALLABLE_CLASS); |
|
| 190 | - $this->registerCallablesFromConfig($xConfig, |
|
| 191 | - 'directories', 'path', Jaxon::CALLABLE_DIR); |
|
| 185 | +// Register functions, classes and directories |
|
| 186 | +$this->registerCallablesFromConfig($xConfig, |
|
| 187 | +'functions', 'name', Jaxon::CALLABLE_FUNCTION); |
|
| 188 | +$this->registerCallablesFromConfig($xConfig, |
|
| 189 | +'classes', 'name', Jaxon::CALLABLE_CLASS); |
|
| 190 | +$this->registerCallablesFromConfig($xConfig, |
|
| 191 | +'directories', 'path', Jaxon::CALLABLE_DIR); |
|
| 192 | 192 | |
| 193 | - // Unset the current config. |
|
| 194 | - $this->xRegistry->unsetPackageConfig(); |
|
| 193 | +// Unset the current config. |
|
| 194 | +$this->xRegistry->unsetPackageConfig(); |
|
| 195 | 195 | |
| 196 | - // Register the view namespaces |
|
| 197 | - // Note: the $xUserConfig can provide a "template" option, which is used to customize |
|
| 198 | - // the user defined view namespaces. That's why it is needed here. |
|
| 199 | - $this->xViewRenderer->addNamespaces($xConfig); |
|
| 200 | - // Save items in the DI container |
|
| 201 | - $this->updateContainer($xConfig); |
|
| 202 | - // Register the exception handlers |
|
| 203 | - $this->registerExceptionHandlers($xConfig); |
|
| 204 | - } |
|
| 196 | +// Register the view namespaces |
|
| 197 | +// Note: the $xUserConfig can provide a "template" option, which is used to customize |
|
| 198 | +// the user defined view namespaces. That's why it is needed here. |
|
| 199 | +$this->xViewRenderer->addNamespaces($xConfig); |
|
| 200 | +// Save items in the DI container |
|
| 201 | +$this->updateContainer($xConfig); |
|
| 202 | +// Register the exception handlers |
|
| 203 | +$this->registerExceptionHandlers($xConfig); |
|
| 204 | +} |
|
| 205 | 205 | |
| 206 | - /** |
|
| 206 | +/** |
|
| 207 | 207 | * Get the options provided by the package library |
| 208 | 208 | * |
| 209 | 209 | * @param class-string $sClassName The package class |
@@ -211,27 +211,27 @@ discard block |
||
| 211 | 211 | * @return Config |
| 212 | 212 | * @throws SetupException |
| 213 | 213 | */ |
| 214 | - private function getPackageLibConfig(string $sClassName): Config |
|
| 215 | - { |
|
| 216 | - // $this->aPackages contains packages config file paths. |
|
| 217 | - $aLibOptions = $sClassName::config(); |
|
| 218 | - if(is_string($aLibOptions)) |
|
| 219 | - { |
|
| 220 | - // A string is supposed to be the path to a config file. |
|
| 221 | - $aLibOptions = $this->xConfigManager->read($aLibOptions); |
|
| 222 | - } |
|
| 223 | - elseif(!is_array($aLibOptions)) |
|
| 224 | - { |
|
| 225 | - // Otherwise, anything else than an array is not accepted. |
|
| 226 | - $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
| 227 | - throw new SetupException($sMessage); |
|
| 228 | - } |
|
| 229 | - // Add the package name to the config |
|
| 230 | - $aLibOptions['package'] = $sClassName; |
|
| 231 | - return $this->xConfigManager->newConfig($aLibOptions); |
|
| 232 | - } |
|
| 214 | +private function getPackageLibConfig(string $sClassName): Config |
|
| 215 | +{ |
|
| 216 | +// $this->aPackages contains packages config file paths. |
|
| 217 | +$aLibOptions = $sClassName::config(); |
|
| 218 | +if(is_string($aLibOptions)) |
|
| 219 | +{ |
|
| 220 | +// A string is supposed to be the path to a config file. |
|
| 221 | +$aLibOptions = $this->xConfigManager->read($aLibOptions); |
|
| 222 | +} |
|
| 223 | +elseif(!is_array($aLibOptions)) |
|
| 224 | +{ |
|
| 225 | +// Otherwise, anything else than an array is not accepted. |
|
| 226 | +$sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
| 227 | +throw new SetupException($sMessage); |
|
| 228 | +} |
|
| 229 | +// Add the package name to the config |
|
| 230 | +$aLibOptions['package'] = $sClassName; |
|
| 231 | +return $this->xConfigManager->newConfig($aLibOptions); |
|
| 232 | +} |
|
| 233 | 233 | |
| 234 | - /** |
|
| 234 | +/** |
|
| 235 | 235 | * Register a package |
| 236 | 236 | * |
| 237 | 237 | * @param class-string $sClassName The package class |
@@ -240,27 +240,27 @@ discard block |
||
| 240 | 240 | * @return void |
| 241 | 241 | * @throws SetupException |
| 242 | 242 | */ |
| 243 | - public function registerPackage(string $sClassName, array $aUserOptions = []): void |
|
| 244 | - { |
|
| 245 | - $sClassName = trim($sClassName, '\\ '); |
|
| 246 | - if(!is_subclass_of($sClassName, AbstractPackage::class)) |
|
| 247 | - { |
|
| 248 | - $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
| 249 | - throw new SetupException($sMessage); |
|
| 250 | - } |
|
| 243 | +public function registerPackage(string $sClassName, array $aUserOptions = []): void |
|
| 244 | +{ |
|
| 245 | +$sClassName = trim($sClassName, '\\ '); |
|
| 246 | +if(!is_subclass_of($sClassName, AbstractPackage::class)) |
|
| 247 | +{ |
|
| 248 | +$sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
| 249 | +throw new SetupException($sMessage); |
|
| 250 | +} |
|
| 251 | 251 | |
| 252 | - // Register the declarations in the package config. |
|
| 253 | - $xAppConfig = $this->getPackageLibConfig($sClassName); |
|
| 254 | - $this->registerItemsFromConfig($xAppConfig); |
|
| 252 | +// Register the declarations in the package config. |
|
| 253 | +$xAppConfig = $this->getPackageLibConfig($sClassName); |
|
| 254 | +$this->registerItemsFromConfig($xAppConfig); |
|
| 255 | 255 | |
| 256 | - // Register the package and its options in the DI |
|
| 257 | - $this->di->registerPackage($sClassName, $aUserOptions); |
|
| 256 | +// Register the package and its options in the DI |
|
| 257 | +$this->di->registerPackage($sClassName, $aUserOptions); |
|
| 258 | 258 | |
| 259 | - // Register the package as a code generator. |
|
| 260 | - $this->xCodeGenerator->addCodeGenerator($sClassName, 500); |
|
| 261 | - } |
|
| 259 | +// Register the package as a code generator. |
|
| 260 | +$this->xCodeGenerator->addCodeGenerator($sClassName, 500); |
|
| 261 | +} |
|
| 262 | 262 | |
| 263 | - /** |
|
| 263 | +/** |
|
| 264 | 264 | * Get a package instance |
| 265 | 265 | * |
| 266 | 266 | * @template T of AbstractPackage |
@@ -268,42 +268,42 @@ discard block |
||
| 268 | 268 | * |
| 269 | 269 | * @return T|null |
| 270 | 270 | */ |
| 271 | - public function getPackage(string $sClassName): ?AbstractPackage |
|
| 272 | - { |
|
| 273 | - $sClassName = trim($sClassName, '\\ '); |
|
| 274 | - return $this->di->h($sClassName) ? $this->di->g($sClassName) : null; |
|
| 275 | - } |
|
| 271 | +public function getPackage(string $sClassName): ?AbstractPackage |
|
| 272 | +{ |
|
| 273 | +$sClassName = trim($sClassName, '\\ '); |
|
| 274 | +return $this->di->h($sClassName) ? $this->di->g($sClassName) : null; |
|
| 275 | +} |
|
| 276 | 276 | |
| 277 | - /** |
|
| 277 | +/** |
|
| 278 | 278 | * Read and set Jaxon options from the config |
| 279 | 279 | * |
| 280 | 280 | * @return void |
| 281 | 281 | * @throws SetupException |
| 282 | 282 | */ |
| 283 | - public function registerFromConfig(): void |
|
| 284 | - { |
|
| 285 | - $xAppConfig = $this->xConfigManager->getAppConfig(); |
|
| 286 | - $this->registerItemsFromConfig($xAppConfig); |
|
| 283 | +public function registerFromConfig(): void |
|
| 284 | +{ |
|
| 285 | +$xAppConfig = $this->xConfigManager->getAppConfig(); |
|
| 286 | +$this->registerItemsFromConfig($xAppConfig); |
|
| 287 | 287 | |
| 288 | - // Register packages |
|
| 289 | - $aPackageConfig = $xAppConfig->getOption('packages', []); |
|
| 290 | - foreach($aPackageConfig as $xKey => $xValue) |
|
| 291 | - { |
|
| 292 | - if(is_integer($xKey) && is_string($xValue)) |
|
| 293 | - { |
|
| 294 | - // Register a package without options |
|
| 295 | - $sClassName = $xValue; |
|
| 296 | - $this->registerPackage($sClassName); |
|
| 297 | - continue; |
|
| 298 | - } |
|
| 288 | +// Register packages |
|
| 289 | +$aPackageConfig = $xAppConfig->getOption('packages', []); |
|
| 290 | +foreach($aPackageConfig as $xKey => $xValue) |
|
| 291 | +{ |
|
| 292 | +if(is_integer($xKey) && is_string($xValue)) |
|
| 293 | +{ |
|
| 294 | + // Register a package without options |
|
| 295 | + $sClassName = $xValue; |
|
| 296 | + $this->registerPackage($sClassName); |
|
| 297 | + continue; |
|
| 298 | +} |
|
| 299 | 299 | |
| 300 | - if(is_string($xKey) && is_array($xValue)) |
|
| 301 | - { |
|
| 302 | - // Register a package with options |
|
| 303 | - $sClassName = $xKey; |
|
| 304 | - $aPkgOptions = $xValue; |
|
| 305 | - $this->registerPackage($sClassName, $aPkgOptions); |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - } |
|
| 300 | +if(is_string($xKey) && is_array($xValue)) |
|
| 301 | +{ |
|
| 302 | + // Register a package with options |
|
| 303 | + $sClassName = $xKey; |
|
| 304 | + $aPkgOptions = $xValue; |
|
| 305 | + $this->registerPackage($sClassName, $aPkgOptions); |
|
| 306 | +} |
|
| 307 | +} |
|
| 308 | +} |
|
| 309 | 309 | } |