Skip to content

Commit

Permalink
Merge branch 'main' of github.com:helsingborg-stad/component-library
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thulin committed May 13, 2024
2 parents 793778d + e4fd212 commit 1ec589b
Show file tree
Hide file tree
Showing 29 changed files with 299 additions and 87 deletions.
3 changes: 2 additions & 1 deletion component-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: component-library
* Plugin URI: (#plugin_url#)
* Description: A library of blade components
* Version: 4.6.30
* Version: 4.10.0
* Author: Eric Rosenborg
* Author URI: (#plugin_author_url#)
* License: MIT
Expand Down Expand Up @@ -33,4 +33,5 @@
load_plugin_textdomain('component-library', false, plugin_basename(dirname(__FILE__)) . '/languages');
}

require_once COMPONENTLIBRARY_PATH . 'load.php';
require_once COMPONENTLIBRARY_PATH . 'Public.php';
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require": {
"php": "^8.0",
"helsingborg-stad/blade": "^3.4.3"
"helsingborg-stad/blade": "^3.5.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
Expand All @@ -26,5 +26,5 @@
"ComponentLibrary\\": "source/php/"
}
},
"version": "4.6.30"
"version": "4.10.0"
}
67 changes: 59 additions & 8 deletions load.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,63 @@
<?php
//Define basepath
define('BCL_BASEPATH', dirname(__FILE__) . '/');

//Autload controllers etc
if(file_exists(BCL_BASEPATH . 'vendor/autoload.php')) {
require_once BCL_BASEPATH . 'vendor/autoload.php';
/**
* Class ComponentLibraryAutoLoad
*
* This class is responsible for autoloading the Component Library.
*/
Class ComponentLibraryAutoLoad {

/**
* ComponentLibraryAutoLoad constructor.
*
* Initializes the autoloader if it is not already loaded.
*/
public function __construct() {
if(!$this->isLoaded() && $this->autoloadExists()) {
$this->load();
}
}

/**
* Get the path of a file or directory.
*
* @param string $append (optional) The path to append to the base path.
* @return string The full path.
*/
private function getPath(string $append = ''): string
{
return dirname(__FILE__) . '/' . $append;
}

/**
* Check if the Component Library is already loaded.
*
* @return bool True if the Component Library is loaded, false otherwise.
*/
private function isLoaded(): bool
{
return class_exists('ComponentLibrary\Init');
}

/**
* Check if the autoloader file exists.
*
* @return bool True if the autoloader file exists, false otherwise.
*/
private function autoloadExists(): bool
{
return file_exists($this->getPath('vendor/autoload.php'));
}

/**
* Load the Component Library.
*
* @return bool True if the Component Library is successfully loaded, false otherwise.
*/
private function load(): void
{
require_once $this->getPath('vendor/autoload.php');
}
}

//Include base classes (TODO: Use autoload instead)
require_once BCL_BASEPATH . 'source/php/Init.php';
require_once BCL_BASEPATH . 'source/php/Register.php';
new ComponentLibraryAutoLoad();
8 changes: 8 additions & 0 deletions source/php/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ComponentLibrary\Cache;

interface CacheInterface {
public function get(string $key, ?string $group = null): mixed;
public function set(string $key, mixed $data, ?string $group = null): void;
}
35 changes: 35 additions & 0 deletions source/php/Cache/StaticCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ComponentLibrary\Cache;

class StaticCache implements CacheInterface {

private static $cache = [];

public function __construct() {}

public function get(string $key, ?string $group = null): mixed {
if ($group && isset(self::$cache[$group][$key])) {
return self::$cache[$group][$key];
}

if ($key && isset(self::$cache[$key])) {
return self::$cache[$key];
}

return null;
}

public function set(string $key, mixed $data, ?string $group = null): void {
if ($group) {
self::$cache[$group] = self::$cache[$group] ?? [];
self::$cache[$group][$key] = $data;

return;
}

if ($key) {
self::$cache[$key] = $data;
}
}
}
25 changes: 25 additions & 0 deletions source/php/Cache/TrySetWpCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ComponentLibrary\Cache;

use ComponentLibrary\Cache\CacheInterface;
use ComponentLibrary\Cache\WpCache;

Class TrySetWpCache implements CacheInterface
{
public function __construct(private CacheInterface $cache) {
if (function_exists('wp_cache_set')) {
$this->cache = new WpCache();
}

return $this->cache;
}

public function set(string $key, mixed $data, ?string $group = null): void {
$this->cache->set($key, $data, $group);
}

public function get(string $key, ?string $group = null): mixed {
return $this->cache->get($key, $group);
}
}
15 changes: 15 additions & 0 deletions source/php/Cache/WpCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ComponentLibrary\Cache;

class WpCache implements CacheInterface {
public function __construct(){}

public function get(string $key, ?string $group = null): mixed {
return wp_cache_get($key, $group);
}

public function set(string $key, mixed $data, ?string $group = null): void {
wp_cache_set($key, $data, $group ?? "");
}
}
4 changes: 3 additions & 1 deletion source/php/Component/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ComponentLibrary\Component;

use ComponentLibrary\Cache\CacheInterface;

class BaseController
{
/**
Expand Down Expand Up @@ -29,7 +31,7 @@ class BaseController
/**
* Run init
*/
public function __construct($data)
public function __construct($data, protected CacheInterface $cache)
{
//Load input data
if (!is_null($data) && is_array($data)) {
Expand Down
2 changes: 1 addition & 1 deletion source/php/Component/Block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"types": {
"heading": "string",
"content": "string|array",
"meta": "string",
"meta": "string|array",
"secondaryMeta": "string|array",
"image": "array",
"link": "string",
Expand Down
2 changes: 1 addition & 1 deletion source/php/Component/Button/button.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"type": "string",
"style": "string",
"shape": "string",
"href": "string",
"href": "string|NULL",
"target": "string",
"componentElement":"string",
"labelElement":"string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

@if($icon && !empty($displayIcon))
<div class="c-collection__icon">
@icon(['icon' => $icon, 'size' => 'md'])
@icon([
'icon' => $icon,
'size' => 'md',
'decorative' => true
])
@endicon
</div>
@endif
Expand Down
Loading

0 comments on commit 1ec589b

Please sign in to comment.