Skip to content

Commit

Permalink
Make autoload of pages, commands and themes configurable
Browse files Browse the repository at this point in the history
Not everyone might want them to autoload
  • Loading branch information
slawkens committed Jun 13, 2024
1 parent 47a19e8 commit c1d4b4f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
8 changes: 7 additions & 1 deletion plugins/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@
"redirect_to": "account/manage"
}
},
"settings": "plugins/your-plugin-folder/settings.php"
"settings": "plugins/your-plugin-folder/settings.php",
"autoload": {
"pages": true,
"pagesSubFolders": false,
"commands": true,
"themes": true
}
}
77 changes: 53 additions & 24 deletions system/src/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,42 @@ public static function getRoutes()
}
}

//
// Get all plugins/*/pages/*.php pages
//
$pluginPages = glob(PLUGINS . $plugin['filename'] . '/pages/*.php');
foreach ($pluginPages as $file) {
$file = str_replace(PLUGINS, 'plugins/', $file);
$name = pathinfo($file, PATHINFO_FILENAME);

if (!isset($duplicates[$name])) {
$routes[] = [['get', 'post'], $name, $file, $priority];
$duplicates[$name] = true;
}
}

//
// Get all plugins/*/pages/subFolder/*.php pages
//
$pluginPagesSubFolders = glob(PLUGINS . $plugin['filename'] . '/pages/*', GLOB_ONLYDIR);
foreach ($pluginPagesSubFolders as $folder) {
$folderName = pathinfo($folder, PATHINFO_FILENAME);

$subFiles = glob(PLUGINS . $plugin['filename'] . '/pages/' . $folderName . '/*.php');
foreach ($subFiles as $file) {
if (self::getAutoLoadOption($plugin, 'pages', true)) {
//
// Get all plugins/*/pages/*.php pages
//
$pluginPages = glob(PLUGINS . $plugin['filename'] . '/pages/*.php');
foreach ($pluginPages as $file) {
$file = str_replace(PLUGINS, 'plugins/', $file);
$name = $folderName . '/' . pathinfo($file, PATHINFO_FILENAME);
$name = pathinfo($file, PATHINFO_FILENAME);

if (!isset($duplicates[$name])) {
$routes[] = [['get', 'post'], $name, $file, $priority];
$duplicates[$name] = true;
}
}
}

if (self::getAutoLoadOption($plugin, 'pagesSubFolders', true)) {
//
// Get all plugins/*/pages/subFolder/*.php pages
//
$pluginPagesSubFolders = glob(PLUGINS . $plugin['filename'] . '/pages/*', GLOB_ONLYDIR);
foreach ($pluginPagesSubFolders as $folder) {
$folderName = pathinfo($folder, PATHINFO_FILENAME);

$subFiles = glob(PLUGINS . $plugin['filename'] . '/pages/' . $folderName . '/*.php');
foreach ($subFiles as $file) {
$file = str_replace(PLUGINS, 'plugins/', $file);
$name = $folderName . '/' . pathinfo($file, PATHINFO_FILENAME);

if (!isset($duplicates[$name])) {
$routes[] = [['get', 'post'], $name, $file, $priority];
$duplicates[$name] = true;
}
}
}
}
}

usort($routes, function ($a, $b)
Expand Down Expand Up @@ -158,6 +162,10 @@ public static function getThemes()

$themes = [];
foreach(self::getAllPluginsJson() as $plugin) {
if (!self::getAutoLoadOption($plugin, 'themes', true)) {
continue;
}

$pluginThemes = glob(PLUGINS . $plugin['filename'] . '/themes/*', GLOB_ONLYDIR);
foreach ($pluginThemes as $path) {
$path = str_replace(PLUGINS, 'plugins/', $path);
Expand Down Expand Up @@ -186,6 +194,10 @@ public static function getCommands()

$commands = [];
foreach(self::getAllPluginsJson() as $plugin) {
if (!self::getAutoLoadOption($plugin, 'commands', true)) {
continue;
}

$pluginCommands = glob(PLUGINS . $plugin['filename'] . '/commands/*.php');
foreach ($pluginCommands as $path) {
$commands[] = $path;
Expand Down Expand Up @@ -792,4 +804,21 @@ public static function installMenus($templateName, $categories, $clearOld = true
}
}
}

private static function getAutoLoadOption(array $plugin, string $optionName, bool $default = true)
{
if (isset($plugin['autoload'])) {
$autoload = $plugin['autoload'];
if (is_array($autoload)) {
if (isset($autoload[$optionName])) {
return getBoolean($autoload[$optionName]);
}
}
else if (is_bool($autoload)) {
return $autoload;
}
}

return $default;
}
}

0 comments on commit c1d4b4f

Please sign in to comment.