From d0ef6cb1d4702a1d6817967a3784f412c098d06e Mon Sep 17 00:00:00 2001 From: Glynn Quelch Date: Thu, 4 Jan 2024 15:39:36 +0000 Subject: [PATCH 1/5] remove isset() and replaced with array_key_exists() --- src/Services/View/Component/Component_Compiler.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Services/View/Component/Component_Compiler.php b/src/Services/View/Component/Component_Compiler.php index 7302589..2704eb9 100644 --- a/src/Services/View/Component/Component_Compiler.php +++ b/src/Services/View/Component/Component_Compiler.php @@ -49,7 +49,9 @@ class Component_Compiler { */ private array $component_aliases = array(); - /** @param array $component_aliases */ + /** + * @param array $component_aliases +*/ public function __construct( string $component_base_path = '', array $component_aliases = array() ) { $this->component_base_path = $component_base_path; $this->component_aliases = \apply_filters( Hooks::COMPONENT_ALIASES, $component_aliases ); @@ -76,7 +78,7 @@ private function get_component_path( Component $component ): string { // Check aliases. $aliases = \apply_filters( Hooks::COMPONENT_ALIASES, $this->component_aliases ); - if ( isset( $aliases[ get_class( $component ) ] ) ) { + if ( \array_key_exists( get_class( $component ), $this->component_aliases ) ) { return esc_attr( $aliases[ get_class( $component ) ] ); } @@ -104,7 +106,7 @@ private function get_component_path( Component $component ): string { /** * Attempts to extract a defined Annotation from component class doc block. * - * @param string $annotation + * @param string $annotation * @param Component $component * @return string|null */ From c5a7f1d701c44678177ec001dabd23d266ea4a33 Mon Sep 17 00:00:00 2001 From: Glynn Quelch Date: Thu, 4 Jan 2024 15:47:27 +0000 Subject: [PATCH 2/5] Formatting --- src/Services/View/Component/Component_Compiler.php | 4 ++-- tests/Fixtures/views/components/alias-dir/some-view.php | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 tests/Fixtures/views/components/alias-dir/some-view.php diff --git a/src/Services/View/Component/Component_Compiler.php b/src/Services/View/Component/Component_Compiler.php index 2704eb9..b9fc7da 100644 --- a/src/Services/View/Component/Component_Compiler.php +++ b/src/Services/View/Component/Component_Compiler.php @@ -50,8 +50,8 @@ class Component_Compiler { private array $component_aliases = array(); /** - * @param array $component_aliases -*/ + * @param array $component_aliases + */ public function __construct( string $component_base_path = '', array $component_aliases = array() ) { $this->component_base_path = $component_base_path; $this->component_aliases = \apply_filters( Hooks::COMPONENT_ALIASES, $component_aliases ); diff --git a/tests/Fixtures/views/components/alias-dir/some-view.php b/tests/Fixtures/views/components/alias-dir/some-view.php new file mode 100644 index 0000000..e901a15 --- /dev/null +++ b/tests/Fixtures/views/components/alias-dir/some-view.php @@ -0,0 +1,2 @@ + +

ALIAS component( $span ); ?>

\ No newline at end of file From f7cef6f72f401ae40586f2e3f0aaf36d61184b28 Mon Sep 17 00:00:00 2001 From: Glynn Quelch Date: Thu, 4 Jan 2024 15:53:53 +0000 Subject: [PATCH 3/5] Now uses the full path if defined --- src/Services/View/PHP_Engine.php | 9 ++++-- tests/Integration/View/Test_Use_View.php | 38 +++++++++++++++++++++--- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/Services/View/PHP_Engine.php b/src/Services/View/PHP_Engine.php index 064d0ac..367495c 100644 --- a/src/Services/View/PHP_Engine.php +++ b/src/Services/View/PHP_Engine.php @@ -100,8 +100,12 @@ public function component( Component $component, bool $print = true ): ?string { // Compile the component. $compiled = $this->component_compiler->compile( $component ); // @phpstan-ignore-line, checked above. - $template = $this->maybe_resolve_dot_notation( $compiled->template() ); - $view = sprintf( '%s%s%s.php', $this->base_view_path, \DIRECTORY_SEPARATOR, trim( $template ) ); + $template = $compiled->template(); + + $view = file_exists( $template ) + ? $template + : sprintf( '%s%s%s.php', $this->base_view_path, \DIRECTORY_SEPARATOR, trim( $this->maybe_resolve_dot_notation( $template ) ) ); + if ( $print ) { print( $this->render_buffer( $view, $compiled->data() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return null; @@ -110,7 +114,6 @@ public function component( Component $component, bool $print = true ): ?string { } } - /** * Renders a view Model * diff --git a/tests/Integration/View/Test_Use_View.php b/tests/Integration/View/Test_Use_View.php index 45b575b..dcbbf21 100644 --- a/tests/Integration/View/Test_Use_View.php +++ b/tests/Integration/View/Test_Use_View.php @@ -14,6 +14,7 @@ use WP_UnitTestCase; use PinkCrab\Loader\Hook_Loader; +use PinkCrab\Perique\Application\Hooks; use PinkCrab\Perique\Application\App_Factory; use PinkCrab\Perique\Interfaces\DI_Container; use PinkCrab\Perique\Services\Dice\PinkCrab_Dice; @@ -33,7 +34,6 @@ * @group integration * @group view * @group components - * */ class Test_Use_View extends WP_UnitTestCase { @@ -46,7 +46,6 @@ class Test_Use_View extends WP_UnitTestCase { public function tear_down(): void { parent::tear_down(); self::unset_app_instance(); - } public function set_up() { @@ -54,7 +53,9 @@ public function set_up() { self::unset_app_instance(); } - /** @testdox By default the default component path should be {view_path}/component */ + /** + * @testdox By default the default component path should be {view_path}/component +*/ public function test_render_component_with_base_path() { $app = ( new App_Factory( \FIXTURES_PATH ) ) ->default_setup() @@ -65,6 +66,35 @@ public function test_render_component_with_base_path() { array( 'component' => new P_Tag_Component( 'test', new Span( 'span-class', 'span-content' ) ) ), false ); - $this->assertEquals('

span-content

', $output); + $this->assertEquals( '

span-content

', $output ); + } + + /** + * @testdox It should be possible to use a component alias and have it use the full path. + * @see https://github.com/Pink-Crab/Perique-Framework/issues/182 + */ + public function test_render_component_with_alias() { + add_filter( + Hooks::COMPONENT_ALIASES, + function ( array $aliases ): array { + $aliases[ P_Tag_Component::class ] = \FIXTURES_PATH . '/views/components/alias-dir/some-view.php'; + return $aliases; + } + ); + + $app = ( new App_Factory( \FIXTURES_PATH ) ) + ->default_setup() + ->boot(); + + $output = $app::view()->render( + 'render-component', + array( 'component' => new P_Tag_Component( 'test', new Span( 'span-class', 'span-content' ) ) ), + false + ); + + // Remove the filter. + add_filter( Hooks::COMPONENT_ALIASES, fn ( array $aliases ): array => array() ); + + $this->assertStringContainsString( '

ALIAS span-content

', $output ); } } From dc46d47b42adc3ae34fb3f758ac92018a57b6db0 Mon Sep 17 00:00:00 2001 From: Glynn Quelch Date: Thu, 4 Jan 2024 16:19:32 +0000 Subject: [PATCH 4/5] bumped --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ba6aba4..1d03931 100644 --- a/README.md +++ b/README.md @@ -344,12 +344,12 @@ $view->render('path/to/file',['var' => 'foo']); We have a number of hooks you can use to extend or modify how the app works. All of our internal hooks have pinkcrab/pf/app/ prefix, but we have a class of constants you can use `PinkCrab\Perique\Application\Hooks:: APP_INIT_*` -* `Hooks:: APP_INIT_PRE_BOOT` -* `Hooks:: APP_INIT_PRE_REGISTRATION` -* `Hooks:: APP_INIT_POST_REGISTRATION` -* `Hooks:: APP_INIT_CONFIG_VALUES` -* `Hooks:: APP_INIT_REGISTRATION_CLASS_LIST` -* `Hooks:: APP_INIT_SET_DI_RULES` +* `Hooks::APP_INIT_PRE_BOOT` +* `Hooks::APP_INIT_PRE_REGISTRATION` +* `Hooks::APP_INIT_POST_REGISTRATION` +* `Hooks::APP_INIT_CONFIG_VALUES` +* `Hooks::APP_INIT_REGISTRATION_CLASS_LIST` +* `Hooks::APP_INIT_SET_DI_RULES` * `Hooks::COMPONENT_ALIASES` * `Hooks::MODULE_MANAGER` @@ -363,6 +363,7 @@ http://www.opensource.org/licenses/mit-license.html ## Change Log ## +* 2.0.4 - Fix bugs where component paths were not being resolved correctly when added using the alias filter. * 2.0.3 - Add PHP8.3 to test suites * 2.0.2 - Test suites updated to include WP6.3 & 6.4 * 2.0.1 - Update dev dependencies and remove plugin.php from root of repo. From 9fe239bdf5f078369ade8b71aeb13dc05bed94e1 Mon Sep 17 00:00:00 2001 From: Glynn Quelch Date: Thu, 4 Jan 2024 16:20:42 +0000 Subject: [PATCH 5/5] Formatting --- src/Services/View/PHP_Engine.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Services/View/PHP_Engine.php b/src/Services/View/PHP_Engine.php index 367495c..d2cfcdd 100644 --- a/src/Services/View/PHP_Engine.php +++ b/src/Services/View/PHP_Engine.php @@ -103,8 +103,8 @@ public function component( Component $component, bool $print = true ): ?string { $template = $compiled->template(); $view = file_exists( $template ) - ? $template - : sprintf( '%s%s%s.php', $this->base_view_path, \DIRECTORY_SEPARATOR, trim( $this->maybe_resolve_dot_notation( $template ) ) ); + ? $template + : sprintf( '%s%s%s.php', $this->base_view_path, \DIRECTORY_SEPARATOR, trim( $this->maybe_resolve_dot_notation( $template ) ) ); if ( $print ) { print( $this->render_buffer( $view, $compiled->data() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped