Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: require php 8.3 #371

Merged
merged 12 commits into from
Feb 20, 2024
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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') != '' }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Run Tests

on:
push:
branches: [ "master" ]
branches: [ "main" ]
pull_request:
branches: [ "master" ]
branches: [ "main" ]

permissions:
contents: read
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions component-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* Domain Path: /languages
*/

use ComponentLibrary\Init as ComponentLibraryInit;

// Protect agains direct file access
if (! defined('WPINC')) {
//die;
Expand All @@ -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';
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
"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": {
"ComponentLibrary\\": "source/php/"
}
},
"version": "3.17.5"
}
}
2 changes: 2 additions & 0 deletions source/php/Component/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class BaseController
*/
private $uid = null;

protected array $compParams;

/**
* Run init
*/
Expand Down
2 changes: 1 addition & 1 deletion source/php/Component/Hero/Hero.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 20 additions & 16 deletions source/php/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(
Expand All @@ -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);

Expand Down Expand Up @@ -88,6 +92,6 @@ public function __construct($externalViewPaths) {

public function getEngine()
{
return $this->register->getEngine();
return GlobalBladeService::getInstance();
}
}
45 changes: 10 additions & 35 deletions source/php/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace ComponentLibrary;

use HelsingborgStad\BladeService\BladeServiceInterface;
use Illuminate\Support\Facades\Blade;
use Throwable;

class Register
{
private static $cache = [
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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});
}

Expand Down Expand Up @@ -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) {

Expand Down
Loading