Skip to content

Commit

Permalink
Merge pull request #4 from afbora/release/2.1.0
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
afbora authored Mar 25, 2022
2 parents eb57d3f + 06e2bcf commit 9e63718
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Ahmet Bora
Copyright (c) 2019-2022 Ahmet Bora

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{
"name": "afbora/kirby-loader",
"description": "Kirby plugins loader from multiple roots",
"description": "Plugins loader from multiple roots for Kirby 3.",
"keywords": [],
"version": "2.0.0",
"version": "2.1.0",
"type": "kirby-plugin",
"license": "MIT",
"homepage": "https://github.com/afbora/kirby-loader",
"support": {
"email": "[email protected]"
},
"authors": [
{
"name": "Ahmet Bora",
"email": "[email protected]"
"email": "[email protected]",
"homepage": "https://github.com/afbora/kirby-loader"
}
],
"require": {
Expand Down
54 changes: 31 additions & 23 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,65 @@ public function __construct()
$this->roots = option('afbora.loader.roots', []);
}

public function register()
public function register(): void
{
if (empty($this->roots) === false) {
$index = kirby()->root();

foreach ($this->roots as $root) {
if (is_string($root) === true) {
if (strpos($root, $index) === false) {
$root = $index . DIRECTORY_SEPARATOR . ltrim($root, DIRECTORY_SEPARATOR);
}

$this->pluginsLoader($root);
$root = $this->getRootPath($root);
} elseif (is_a($root, 'Closure') === true) {
$root = $root();
if (strpos($root, $index) === false) {
$root = $index . DIRECTORY_SEPARATOR . ltrim($root, DIRECTORY_SEPARATOR);
}

$this->pluginsLoader($root);
$root = $this->getRootPath($root());
} else {
// not supported type
continue;
}

$this->pluginsLoader($root);
}
}
}

protected function pluginsLoader(string $root): void
{
if ($this->readDir(basename($root), dirname($root)) === false) {
foreach (Dir::read($root) as $dirname) {
$this->readDir($dirname, $root);
// check and register directly plugin directory
$singlePluginRegister = $this->readDir($root);

// read directory and register all plugins in given path
if ($singlePluginRegister === false) {
foreach (Dir::read($root, null, true) as $path) {
$this->readDir($path);
}
}
}

protected function readDir(string $dirname, string $root): bool
protected function readDir(string $root = null): bool
{
if (in_array(substr($dirname, 0, 1), ['.', '_']) === true) {
if (empty($root) === true|| in_array(substr(basename($root), 0, 1), ['.', '..']) === true) {
return false;
}

$dir = $root . DIRECTORY_SEPARATOR . $dirname;
if (is_dir($dir) === false) {
if (is_dir($root) === false) {
return false;
}

$entry = $dir . DIRECTORY_SEPARATOR . 'index.php';
if (is_dir($dir) !== true || is_file($entry) !== true) {
$entry = $root . DIRECTORY_SEPARATOR . 'index.php';
if (is_dir($root) === false || is_file($entry) === false) {
return false;
}

F::loadOnce($entry);

return true;
}

protected function getRootPath(string $root = null): string
{
$index = kirby()->root();

if (is_dir($root) === false && strpos($root, $index) === false) {
$root = $index . DIRECTORY_SEPARATOR . ltrim($root, DIRECTORY_SEPARATOR);
}

return $root;
}
}

0 comments on commit 9e63718

Please sign in to comment.