@@ -53,8 +53,8 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @param string $base_view_path |
| 55 | 55 | */ |
| 56 | - public function __construct( string $base_view_path ) { |
|
| 57 | - $this->base_view_path = $this->verify_view_path( $base_view_path ); |
|
| 56 | + public function __construct(string $base_view_path) { |
|
| 57 | + $this->base_view_path = $this->verify_view_path($base_view_path); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | * @param Component_Compiler $compiler |
| 64 | 64 | * @return void |
| 65 | 65 | */ |
| 66 | - public function set_component_compiler( Component_Compiler $compiler ): void { |
|
| 66 | + public function set_component_compiler(Component_Compiler $compiler): void { |
|
| 67 | 67 | $this->component_compiler = $compiler; |
| 68 | 68 | } |
| 69 | 69 | |
@@ -75,13 +75,13 @@ discard block |
||
| 75 | 75 | * @param boolean $print |
| 76 | 76 | * @return string|null |
| 77 | 77 | */ |
| 78 | - public function render( string $view, iterable $data, bool $print = true ): ?string { |
|
| 79 | - $view = $this->resolve_file_path( $view ); |
|
| 80 | - if ( $print ) { |
|
| 81 | - print( $this->render_buffer( $view, $data ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 78 | + public function render(string $view, iterable $data, bool $print = true): ?string { |
|
| 79 | + $view = $this->resolve_file_path($view); |
|
| 80 | + if ($print) { |
|
| 81 | + print($this->render_buffer($view, $data)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 82 | 82 | return null; |
| 83 | 83 | } else { |
| 84 | - return $this->render_buffer( $view, $data ); |
|
| 84 | + return $this->render_buffer($view, $data); |
|
| 85 | 85 | } |
| 86 | 86 | } |
| 87 | 87 | |
@@ -91,26 +91,26 @@ discard block |
||
| 91 | 91 | * @param Component $component |
| 92 | 92 | * @return string |
| 93 | 93 | */ |
| 94 | - public function component( Component $component, bool $print = true ): ?string { |
|
| 94 | + public function component(Component $component, bool $print = true): ?string { |
|
| 95 | 95 | |
| 96 | 96 | // Throw exception of no compiler passed. |
| 97 | - if ( ! Object_Helper::is_a( $this->component_compiler, Component_Compiler::class ) ) { |
|
| 98 | - throw new Exception( 'No component compiler passed to PHP_Engine' ); |
|
| 97 | + if ( ! Object_Helper::is_a($this->component_compiler, Component_Compiler::class)) { |
|
| 98 | + throw new Exception('No component compiler passed to PHP_Engine'); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Compile the component. |
| 102 | - $compiled = $this->component_compiler->compile( $component ); // @phpstan-ignore-line, checked above. |
|
| 102 | + $compiled = $this->component_compiler->compile($component); // @phpstan-ignore-line, checked above. |
|
| 103 | 103 | $template = $compiled->template(); |
| 104 | 104 | |
| 105 | - $view = file_exists( $template ) |
|
| 105 | + $view = file_exists($template) |
|
| 106 | 106 | ? $template |
| 107 | - : sprintf( '%s%s%s.php', $this->base_view_path, \DIRECTORY_SEPARATOR, trim( $this->maybe_resolve_dot_notation( $template ) ) ); |
|
| 107 | + : sprintf('%s%s%s.php', $this->base_view_path, \DIRECTORY_SEPARATOR, trim($this->maybe_resolve_dot_notation($template))); |
|
| 108 | 108 | |
| 109 | - if ( $print ) { |
|
| 110 | - print( $this->render_buffer( $view, $compiled->data() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 109 | + if ($print) { |
|
| 110 | + print($this->render_buffer($view, $compiled->data())); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 111 | 111 | return null; |
| 112 | 112 | } else { |
| 113 | - return $this->render_buffer( $view, $compiled->data() ); |
|
| 113 | + return $this->render_buffer($view, $compiled->data()); |
|
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | 116 | |
@@ -120,8 +120,8 @@ discard block |
||
| 120 | 120 | * @param View_Model $view_model |
| 121 | 121 | * @return string|null |
| 122 | 122 | */ |
| 123 | - public function view_model( View_Model $view_model, bool $print = true ): ?string { |
|
| 124 | - return $this->render( $view_model->template(), $view_model->data(), $print ); |
|
| 123 | + public function view_model(View_Model $view_model, bool $print = true): ?string { |
|
| 124 | + return $this->render($view_model->template(), $view_model->data(), $print); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
@@ -132,12 +132,12 @@ discard block |
||
| 132 | 132 | * @param boolean $print |
| 133 | 133 | * @return string|null |
| 134 | 134 | */ |
| 135 | - public function partial( string $view, iterable $data = array(), bool $print = true ): ?string { |
|
| 136 | - if ( $print ) { |
|
| 137 | - $this->render( $view, $data, $print ); |
|
| 135 | + public function partial(string $view, iterable $data = array(), bool $print = true): ?string { |
|
| 136 | + if ($print) { |
|
| 137 | + $this->render($view, $data, $print); |
|
| 138 | 138 | return null; |
| 139 | 139 | } else { |
| 140 | - return $this->render( $view, $data, $print ); |
|
| 140 | + return $this->render($view, $data, $print); |
|
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | |
@@ -149,27 +149,27 @@ discard block |
||
| 149 | 149 | * @return string |
| 150 | 150 | * @throws Exception |
| 151 | 151 | */ |
| 152 | - private function render_buffer( string $view, iterable $__data ): string { |
|
| 152 | + private function render_buffer(string $view, iterable $__data): string { |
|
| 153 | 153 | |
| 154 | - if ( ! file_exists( $view ) ) { |
|
| 155 | - throw new Exception( "{$view} doesn't exist" ); |
|
| 154 | + if ( ! file_exists($view)) { |
|
| 155 | + throw new Exception("{$view} doesn't exist"); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | $output = ''; |
| 159 | 159 | ob_start(); |
| 160 | 160 | |
| 161 | 161 | // Set all the data values a parameters. |
| 162 | - foreach ( $__data as $__key => $__value ) { |
|
| 163 | - if ( is_string( $__key ) ) { |
|
| 164 | - ${\wp_strip_all_tags( $__key )} = $__value; |
|
| 162 | + foreach ($__data as $__key => $__value) { |
|
| 163 | + if (is_string($__key)) { |
|
| 164 | + ${\wp_strip_all_tags($__key)} = $__value; |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | // Unset the key and value. |
| 168 | - unset( $__key, $__value ); |
|
| 168 | + unset($__key, $__value); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | // Unset the data. |
| 172 | - unset( $__data ); |
|
| 172 | + unset($__data); |
|
| 173 | 173 | |
| 174 | 174 | include $view; |
| 175 | 175 | $output = ob_get_contents(); |
@@ -183,12 +183,12 @@ discard block |
||
| 183 | 183 | * @param string $filename |
| 184 | 184 | * @return string |
| 185 | 185 | */ |
| 186 | - private function resolve_file_path( string $filename ): string { |
|
| 187 | - $filename = $this->maybe_resolve_dot_notation( $filename ); |
|
| 186 | + private function resolve_file_path(string $filename): string { |
|
| 187 | + $filename = $this->maybe_resolve_dot_notation($filename); |
|
| 188 | 188 | return sprintf( |
| 189 | 189 | '%s%s.php', |
| 190 | 190 | $this->base_view_path, |
| 191 | - trim( $filename ) |
|
| 191 | + trim($filename) |
|
| 192 | 192 | ); |
| 193 | 193 | } |
| 194 | 194 | |
@@ -198,13 +198,13 @@ discard block |
||
| 198 | 198 | * @param string $filename |
| 199 | 199 | * @return string |
| 200 | 200 | */ |
| 201 | - private function maybe_resolve_dot_notation( string $filename ): string { |
|
| 202 | - if ( endsWith( '.php' )( $filename ) ) { |
|
| 203 | - $filename = substr( $filename, 0, -4 ); |
|
| 201 | + private function maybe_resolve_dot_notation(string $filename): string { |
|
| 202 | + if (endsWith('.php')($filename)) { |
|
| 203 | + $filename = substr($filename, 0, -4); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - $parts = explode( '.', $filename ); |
|
| 207 | - $filename = implode( DIRECTORY_SEPARATOR, $parts ); |
|
| 206 | + $parts = explode('.', $filename); |
|
| 207 | + $filename = implode(DIRECTORY_SEPARATOR, $parts); |
|
| 208 | 208 | |
| 209 | 209 | return $filename; |
| 210 | 210 | } |
@@ -216,12 +216,12 @@ discard block |
||
| 216 | 216 | * @return string |
| 217 | 217 | * @throws Exception |
| 218 | 218 | */ |
| 219 | - private function verify_view_path( string $path ): string { |
|
| 220 | - $path = $this->maybe_resolve_dot_notation( $path ); |
|
| 221 | - $path = rtrim( $path, '/' ) . '/'; |
|
| 219 | + private function verify_view_path(string $path): string { |
|
| 220 | + $path = $this->maybe_resolve_dot_notation($path); |
|
| 221 | + $path = rtrim($path, '/') . '/'; |
|
| 222 | 222 | |
| 223 | - if ( ! \is_dir( $path ) ) { |
|
| 224 | - throw new Exception( "{$path} doesn't exist and cant be used as the base view path." ); |
|
| 223 | + if ( ! \is_dir($path)) { |
|
| 224 | + throw new Exception("{$path} doesn't exist and cant be used as the base view path."); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | return $path; |
@@ -52,9 +52,9 @@ discard block |
||
| 52 | 52 | /** |
| 53 | 53 | * @param array<string, string> $component_aliases |
| 54 | 54 | */ |
| 55 | - public function __construct( string $component_base_path = '', array $component_aliases = array() ) { |
|
| 55 | + public function __construct(string $component_base_path = '', array $component_aliases = array()) { |
|
| 56 | 56 | $this->component_base_path = $component_base_path; |
| 57 | - $this->component_aliases = \apply_filters( Hooks::COMPONENT_ALIASES, $component_aliases ); |
|
| 57 | + $this->component_aliases = \apply_filters(Hooks::COMPONENT_ALIASES, $component_aliases); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
@@ -63,8 +63,8 @@ discard block |
||
| 63 | 63 | * @param Component $component |
| 64 | 64 | * @return View_Model |
| 65 | 65 | */ |
| 66 | - public function compile( Component $component ): View_Model { |
|
| 67 | - return new View_Model( $this->get_component_path( $component ), $component->get_variables() ); |
|
| 66 | + public function compile(Component $component): View_Model { |
|
| 67 | + return new View_Model($this->get_component_path($component), $component->get_variables()); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -73,34 +73,34 @@ discard block |
||
| 73 | 73 | * @param Component $component |
| 74 | 74 | * @return string |
| 75 | 75 | */ |
| 76 | - private function get_component_path( Component $component ): string { |
|
| 76 | + private function get_component_path(Component $component): string { |
|
| 77 | 77 | |
| 78 | 78 | // Check aliases. |
| 79 | - $aliases = \apply_filters( Hooks::COMPONENT_ALIASES, $this->component_aliases ); |
|
| 79 | + $aliases = \apply_filters(Hooks::COMPONENT_ALIASES, $this->component_aliases); |
|
| 80 | 80 | |
| 81 | - if ( \array_key_exists( get_class( $component ), $this->component_aliases ) ) { |
|
| 82 | - return esc_attr( $aliases[ get_class( $component ) ] ); |
|
| 81 | + if (\array_key_exists(get_class($component), $this->component_aliases)) { |
|
| 82 | + return esc_attr($aliases[get_class($component)]); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - $from_annotation = $this->get_annotation( 'view', $component ); |
|
| 85 | + $from_annotation = $this->get_annotation('view', $component); |
|
| 86 | 86 | |
| 87 | 87 | // If it does have a path defined, use that. |
| 88 | - if ( ! empty( $from_annotation ) ) { |
|
| 89 | - return \trailingslashit( $this->component_base_path ) . $from_annotation; |
|
| 88 | + if ( ! empty($from_annotation)) { |
|
| 89 | + return \trailingslashit($this->component_base_path) . $from_annotation; |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | // If the component has a defined path |
| 93 | - if ( $component->template() ) { |
|
| 94 | - return \trailingslashit( $this->component_base_path ) . $component->template(); |
|
| 93 | + if ($component->template()) { |
|
| 94 | + return \trailingslashit($this->component_base_path) . $component->template(); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | // Get path based on class name. |
| 98 | - $reflect = new \ReflectionClass( $component ); |
|
| 98 | + $reflect = new \ReflectionClass($component); |
|
| 99 | 99 | $short_name = $reflect->getShortName(); |
| 100 | 100 | // Add space between capitals, make lowercase and replace underscores with dashes. |
| 101 | - $short_name = strtolower( preg_replace( '/(?<!^)[A-Z]/', '$0', $short_name ) ?? '' ); |
|
| 102 | - $short_name = str_replace( '_', '-', $short_name ); |
|
| 103 | - return \trailingslashit( $this->component_base_path ) . $short_name; |
|
| 101 | + $short_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '$0', $short_name) ?? ''); |
|
| 102 | + $short_name = str_replace('_', '-', $short_name); |
|
| 103 | + return \trailingslashit($this->component_base_path) . $short_name; |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
@@ -110,18 +110,18 @@ discard block |
||
| 110 | 110 | * @param Component $component |
| 111 | 111 | * @return string|null |
| 112 | 112 | */ |
| 113 | - private function get_annotation( string $annotation, Component $component ): ?string { |
|
| 114 | - $reflect = new \ReflectionClass( $component ); |
|
| 113 | + private function get_annotation(string $annotation, Component $component): ?string { |
|
| 114 | + $reflect = new \ReflectionClass($component); |
|
| 115 | 115 | $comment = $reflect->getDocComment(); |
| 116 | 116 | |
| 117 | 117 | // if no comment, return null. |
| 118 | - if ( empty( $comment ) ) { |
|
| 118 | + if (empty($comment)) { |
|
| 119 | 119 | return null; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // Check if the comment contains the annotation "@{$annotation}" using regex. |
| 123 | 123 | $pattern = "/@{$annotation}\s+(.*)/"; |
| 124 | - preg_match( $pattern, $comment, $matches ); |
|
| 124 | + preg_match($pattern, $comment, $matches); |
|
| 125 | 125 | |
| 126 | 126 | return $matches[1] ?? null; |
| 127 | 127 | } |