forked from GNUsocial/gnu-social
[COMPOSER][MODULE] Add autoloading rules for components and plugins
This commit is contained in:
parent
9b2db7608b
commit
f5f10890b6
@ -65,7 +65,8 @@
|
|||||||
"files": ["src/Core/I18n/I18n.php"],
|
"files": ["src/Core/I18n/I18n.php"],
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"App\\": "src/",
|
"App\\": "src/",
|
||||||
"Plugin\\": "plugins/enabled/"
|
"Plugin\\": "plugins/",
|
||||||
|
"Component\\": "components/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Core\ModuleManager;
|
||||||
use Symfony\Component\Dotenv\Dotenv;
|
use Symfony\Component\Dotenv\Dotenv;
|
||||||
|
|
||||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
$loader = require dirname(__DIR__) . '/vendor/autoload.php';
|
||||||
|
|
||||||
// Load cached env vars if the .env.local.php file exists
|
// Load cached env vars if the .env.local.php file exists
|
||||||
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
|
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
|
||||||
@ -21,3 +22,5 @@ $_SERVER += $_ENV;
|
|||||||
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'dev';
|
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'dev';
|
||||||
$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV'];
|
$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV'];
|
||||||
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';
|
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';
|
||||||
|
|
||||||
|
ModuleManager::setLoader($loader);
|
||||||
|
93
src/Core/ModuleManager.php
Normal file
93
src/Core/ModuleManager.php
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ License
|
||||||
|
|
||||||
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
|
//
|
||||||
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module and plugin loader code, one of the main features of GNU social
|
||||||
|
*
|
||||||
|
* Loads plugins from `plugins/enabled`, instances them
|
||||||
|
* and hooks its events
|
||||||
|
*
|
||||||
|
* @package GNUsocial
|
||||||
|
* @category Modules
|
||||||
|
*
|
||||||
|
* @author Hugo Sales <hugo@fc.up.pt>
|
||||||
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
||||||
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Core;
|
||||||
|
|
||||||
|
use App\Util\Formatting;
|
||||||
|
use Functional as F;
|
||||||
|
|
||||||
|
class ModuleManager
|
||||||
|
{
|
||||||
|
protected static $loader;
|
||||||
|
public static function setLoader($l)
|
||||||
|
{
|
||||||
|
self::$loader = $l;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected array $modules = [];
|
||||||
|
protected array $events = [];
|
||||||
|
|
||||||
|
public function add(string $fqcn, string $path)
|
||||||
|
{
|
||||||
|
list($type, $module) = preg_split('/\\\\/', $fqcn, 0, PREG_SPLIT_NO_EMPTY);
|
||||||
|
self::$loader->addPsr4("\\{$type}\\{$module}\\", dirname($path));
|
||||||
|
$id = Formatting::camelCaseToSnakeCase($type . '.' . $module);
|
||||||
|
$obj = new $fqcn();
|
||||||
|
$this->modules[$id] = $obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function preRegisterEvents()
|
||||||
|
{
|
||||||
|
foreach ($this->modules as $id => $obj) {
|
||||||
|
F\map(F\select(get_class_methods($obj),
|
||||||
|
F\ary(F\partial_right('App\Util\Formatting::startsWith', 'on'), 1)),
|
||||||
|
function (string $m) use ($obj) {
|
||||||
|
$ev = Formatting::camelCaseToSnakeCase(substr($m, 2));
|
||||||
|
$this->events[$ev] = $this->events[$ev] ?? [];
|
||||||
|
$this->events[$ev][] = [$obj, $m];
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function __set_state($state)
|
||||||
|
{
|
||||||
|
$obj = new self();
|
||||||
|
$obj->modules = $state['modules'];
|
||||||
|
$obj->events = $state['events'];
|
||||||
|
return $obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loadModules()
|
||||||
|
{
|
||||||
|
$f = INSTALLDIR . '/var/cache/module_manager.php';
|
||||||
|
$obj = require $f;
|
||||||
|
foreach ($obj->events as $event => $callables) {
|
||||||
|
foreach ($callables as $callable) {
|
||||||
|
Event::addHandler($event, $callable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,65 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
// {{{ License
|
|
||||||
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
||||||
//
|
|
||||||
// GNU social is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// GNU social is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Affero General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
|
||||||
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module and plugin loader code, one of the main features of GNU social
|
|
||||||
*
|
|
||||||
* Loads plugins from `plugins/enabled`, instances them
|
|
||||||
* and hooks its events
|
|
||||||
*
|
|
||||||
* @package GNUsocial
|
|
||||||
* @category Modules
|
|
||||||
*
|
|
||||||
* @author Hugo Sales <hugo@fc.up.pt>
|
|
||||||
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
|
||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace App\Core;
|
|
||||||
|
|
||||||
use Functional as F;
|
|
||||||
|
|
||||||
abstract class ModulesManager
|
|
||||||
{
|
|
||||||
public static array $modules = [];
|
|
||||||
|
|
||||||
public static function loadModules()
|
|
||||||
{
|
|
||||||
$plugins_paths = glob(INSTALLDIR . '/plugins/*');
|
|
||||||
|
|
||||||
foreach ($plugins_paths as $plugin_path) {
|
|
||||||
$class_name = basename($plugin_path);
|
|
||||||
$fqcn = "Plugin\\{$class_name}\\{$class_name}";
|
|
||||||
$file = "{$plugin_path}/{$class_name}.php";
|
|
||||||
if (file_exists($file)) {
|
|
||||||
require_once $file;
|
|
||||||
$class = new $fqcn;
|
|
||||||
self::$modules[] = $class;
|
|
||||||
|
|
||||||
// Register event handlers
|
|
||||||
$methods = get_class_methods($class);
|
|
||||||
$events = F\select($methods, F\partial_right('App\Util\Formatting::startsWith', 'on'));
|
|
||||||
F\map($events,
|
|
||||||
function (string $m) use ($class) {
|
|
||||||
Event::addHandler(substr($m, 2), [$class, $m]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user