diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ab10f1b0..be54018b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,7 +18,7 @@ jobs: - name: Create Release and bump version files uses: helsingborg-stad/release-wp-plugin-action@1.0.2 with: - php-version: 8.2 + php-version: 8.3 node-version: 20.6.0 build-assets: needs: ['release'] @@ -75,7 +75,7 @@ jobs: if: ${{ hashFiles('composer.json') != '' }} with: tools: composer - php-version: '7.4' + php-version: '7.3' - name: Build PHP if: ${{ hashFiles('composer.json') != '' }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d5377075..c10f2d01 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,9 +2,9 @@ name: Run Tests on: push: - branches: [ "master" ] + branches: [ "main" ] pull_request: - branches: [ "master" ] + branches: [ "main" ] permissions: contents: read @@ -16,7 +16,7 @@ jobs: strategy: matrix: - php-versions: ['7.4'] + php-versions: ['8.0', '8.1', '8.2', '8.3', '8.4'] name: Test running PHP ${{ matrix.php-versions }} steps: diff --git a/component-library.php b/component-library.php index 4a9aba0d..cea81a99 100644 --- a/component-library.php +++ b/component-library.php @@ -13,6 +13,8 @@ * Domain Path: /languages */ +use ComponentLibrary\Init as ComponentLibraryInit; + // Protect agains direct file access if (! defined('WPINC')) { //die; @@ -31,4 +33,10 @@ load_plugin_textdomain('component-library', false, plugin_basename(dirname(__FILE__)) . '/languages'); } +if( function_exists('add_action') ) { + add_action('plugins_loaded', function() { + new ComponentLibraryInit([]); + }); +} + require_once COMPONENTLIBRARY_PATH . 'Public.php'; \ No newline at end of file diff --git a/composer.json b/composer.json index 9210aa72..d566db0d 100644 --- a/composer.json +++ b/composer.json @@ -14,11 +14,11 @@ "test:coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html .coverage/html --coverage-clover .coverage/clover.xml" }, "require": { - "helsingborg-stad/blade-engine-wrapper": "^1.0.0" + "php": "^8.0", + "helsingborg-stad/blade": "^3.2" }, "require-dev": { - "phpunit/phpunit": "^9.6", - "helsingborg-stad/render-blade-view": "^0.0.2" + "phpunit/phpunit": "^9.6" }, "autoload": { "psr-4": { @@ -26,4 +26,4 @@ } }, "version": "3.17.5" -} \ No newline at end of file +} diff --git a/source/php/Component/BaseController.php b/source/php/Component/BaseController.php index f755d74a..ad5f2b80 100644 --- a/source/php/Component/BaseController.php +++ b/source/php/Component/BaseController.php @@ -24,6 +24,8 @@ class BaseController */ private $uid = null; + protected array $compParams; + /** * Run init */ diff --git a/source/php/Component/Hero/Hero.php b/source/php/Component/Hero/Hero.php index d759f8f7..e2c984a4 100644 --- a/source/php/Component/Hero/Hero.php +++ b/source/php/Component/Hero/Hero.php @@ -179,7 +179,7 @@ private function twoColumn($customHeroData) { private function hasContent(): bool { - $stringEmpty = fn ($value): bool => empty(trim($value)); + $stringEmpty = fn ($value): bool => empty(trim($value ?? "")); if (!$stringEmpty($this->data['meta'])) return true; if (!$stringEmpty($this->data['title'])) return true; diff --git a/source/php/Init.php b/source/php/Init.php index bb43acb6..44315d09 100644 --- a/source/php/Init.php +++ b/source/php/Init.php @@ -3,14 +3,14 @@ namespace ComponentLibrary; use ComponentLibrary\Register; -use HelsingborgStad\BladeEngineWrapper as Blade; +use HelsingborgStad\GlobalBladeService\GlobalBladeService; class Init { private $register = null; + private static bool $internalViewPathsAdded = false; public function __construct($externalViewPaths) { - $blade = new Blade(); $paths = array( 'viewPaths' => array(), 'controllerPaths' => array(), @@ -19,9 +19,12 @@ public function __construct($externalViewPaths) { // Add view path to renderer // In this case all components, their controller and view path are located under the same folder structure. // This may differ in a Wordpress child implementation. - $internalPaths = array( - __DIR__ . DIRECTORY_SEPARATOR . 'Component' . DIRECTORY_SEPARATOR, - ); + $internalPaths = array(); + + if( !self::$internalViewPathsAdded ) { + $internalPaths = array( __DIR__ . DIRECTORY_SEPARATOR . 'Component' . DIRECTORY_SEPARATOR ); + self::$internalViewPathsAdded = true; + } // Initialize all view paths so that this library is last $viewPaths = array_unique( @@ -36,19 +39,20 @@ public function __construct($externalViewPaths) { $viewPaths ); } - - if(is_array($viewPaths) && !empty($viewPaths)) { - foreach ($viewPaths as $path) { - $directory = rtrim($path, DIRECTORY_SEPARATOR); - if(is_dir($directory)) { - $blade->addViewPath(rtrim($path, DIRECTORY_SEPARATOR)); - } - } - } else { + + if(!is_array($viewPaths) || empty($viewPaths)) { throw new \Exception("View paths not defined."); + } + + $sanitizedViewPaths = array(); + foreach ($viewPaths as $path) { + $directory = rtrim($path, DIRECTORY_SEPARATOR); + if(is_dir($directory)) { + $sanitizedViewPaths[] = $directory; + } } - $bladeInstance = $blade->instance(); + $bladeInstance = GlobalBladeService::getInstance($sanitizedViewPaths); $this->register = new Register($bladeInstance); @@ -88,6 +92,6 @@ public function __construct($externalViewPaths) { public function getEngine() { - return $this->register->getEngine(); + return GlobalBladeService::getInstance(); } } diff --git a/source/php/Register.php b/source/php/Register.php index f57f2595..78c80bb9 100644 --- a/source/php/Register.php +++ b/source/php/Register.php @@ -2,6 +2,10 @@ namespace ComponentLibrary; +use HelsingborgStad\BladeService\BladeServiceInterface; +use Illuminate\Support\Facades\Blade; +use Throwable; + class Register { private static $cache = [ @@ -17,11 +21,11 @@ class Register public $controllerPaths = []; private $reservedNames = ["data", "class", "list", "lang"]; private $controllers = []; - private $blade = null; + private BladeServiceInterface $blade; - public function __construct($engine) + public function __construct(BladeServiceInterface $bladeService) { - $this->blade = $engine; + $this->blade = $bladeService; } /** @@ -56,10 +60,7 @@ public function add($slug, $defaultArgs, $argsTypes = false, $view = null) 'argsTypes' => (object) $argsTypes ); - //Add include alias - $this->registerComponentAlias($slug); - - // Register view composer + $this->blade->registerComponentDirective( ucfirst($slug) . '.' . $slug, $slug); $this->registerViewComposer($this->data->{$slug}); } @@ -147,36 +148,10 @@ private function getViewName($slug, $view = null): string return $view; } - /** - * Santize string - * @return string The string to be sanitized - */ - private function sanitizeSlug($string): string - { - return preg_replace( - "/[^a-z-]/i", - "", - str_replace(".blade.php", "", $string) - ); - } - - /** - * Registers all components as include aliases - * - * @return bool - */ - private function registerComponentAlias($componentSlug) - { - $this->blade->component( - ucfirst($componentSlug) . '.' . $componentSlug, - $componentSlug - ); - } - - public function registerViewComposer($component) + public function registerViewComposer(object $component) { try { - $this->blade->composer( + $this->blade->registerComponent( ucfirst($component->slug) . '.' . $component->slug, function ($view) use ($component) {