@@ -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( esc_html( "{$view} doesn't exist" ) ); |
|
| 154 | + if ( ! file_exists($view)) { |
|
| 155 | + throw new Exception(esc_html("{$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,11 +216,11 @@ discard block |
||
| 216 | 216 | * @return string |
| 217 | 217 | * @throws Exception |
| 218 | 218 | */ |
| 219 | - private function verify_view_path( string $path ): string { |
|
| 220 | - $path = rtrim( $path, '/' ) . '/'; |
|
| 219 | + private function verify_view_path(string $path): string { |
|
| 220 | + $path = rtrim($path, '/') . '/'; |
|
| 221 | 221 | |
| 222 | - if ( ! \is_dir( $path ) ) { |
|
| 223 | - throw new Exception( esc_html( "{$path} doesn't exist and cant be used as the base view path." ) ); |
|
| 222 | + if ( ! \is_dir($path)) { |
|
| 223 | + throw new Exception(esc_html("{$path} doesn't exist and cant be used as the base view path.")); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | return $path; |