generated from helsingborg-stad/Wordpress-Plugin-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:helsingborg-stad/component-library
- Loading branch information
Showing
29 changed files
with
299 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.