Skip to content

Commit

Permalink
Merge branch 'release/2.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
gin0115 committed Jan 4, 2024
2 parents b4c9611 + 9fe239b commit 94ae69c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions src/Services/View/Component/Component_Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class Component_Compiler {
*/
private array $component_aliases = array();

/** @param array<string, string> $component_aliases */
/**
* @param array<string, string> $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 );
Expand All @@ -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 ) ] );
}

Expand Down Expand Up @@ -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
*/
Expand Down
9 changes: 6 additions & 3 deletions src/Services/View/PHP_Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -110,7 +114,6 @@ public function component( Component $component, bool $print = true ): ?string {
}
}


/**
* Renders a view Model
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/views/components/alias-dir/some-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- This is the same as the P tag, just a unique path -->
<p class="<?php echo $class; ?>">ALIAS <?php $this->component( $span ); ?></p>
38 changes: 34 additions & 4 deletions tests/Integration/View/Test_Use_View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +34,6 @@
* @group integration
* @group view
* @group components
*
*/
class Test_Use_View extends WP_UnitTestCase {

Expand All @@ -46,15 +46,16 @@ class Test_Use_View extends WP_UnitTestCase {
public function tear_down(): void {
parent::tear_down();
self::unset_app_instance();

}

public function set_up() {
parent::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()
Expand All @@ -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('<p class="test"><span class="span-class">span-content</span></p>', $output);
$this->assertEquals( '<p class="test"><span class="span-class">span-content</span></p>', $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( '<p class="test">ALIAS <span class="span-class">span-content</span></p>', $output );
}
}

0 comments on commit 94ae69c

Please sign in to comment.