diff --git a/config/services.yaml b/config/services.yaml index 97d023375a..d2eb7df34d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -23,12 +23,12 @@ services: resource: '../src/Controller' tags: ['controller.service_arguments'] - App\Util\RouteLoader: + App\Core\RouteLoader: tags: ['routing.loader'] # Wrapper arround Doctrine's StaticPHP metadata driver - app.util.schemadef_driver: - class: App\Util\SchemaDefDriver + app.core.schemadef_driver: + class: App\Core\SchemaDefDriver arguments: - '%kernel.project_dir%/src/Entity' diff --git a/src/Controller/NetworkPublic.php b/src/Controller/NetworkPublic.php index 51a52ae87d..b3179822d3 100644 --- a/src/Controller/NetworkPublic.php +++ b/src/Controller/NetworkPublic.php @@ -30,7 +30,7 @@ namespace App\Controller; -use App\Util\GSEvent as Event; +use App\Core\GSEvent as Event; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class NetworkPublic extends AbstractController diff --git a/src/Util/GNUsocial.php b/src/Core/GNUsocial.php similarity index 99% rename from src/Util/GNUsocial.php rename to src/Core/GNUsocial.php index 9610717c6c..03ea724f00 100644 --- a/src/Util/GNUsocial.php +++ b/src/Core/GNUsocial.php @@ -38,7 +38,7 @@ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -namespace App\Util; +namespace App\Core; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; diff --git a/src/Util/GSEvent.php b/src/Core/GSEvent.php similarity index 99% rename from src/Util/GSEvent.php rename to src/Core/GSEvent.php index 71bf6b1c7f..0a349e1f23 100644 --- a/src/Util/GSEvent.php +++ b/src/Core/GSEvent.php @@ -29,7 +29,7 @@ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -namespace App\Util; +namespace App\Core; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -43,7 +43,6 @@ abstract class GSEvent * bool next - Allow the other handlers to process the event */ public const stop = false; - public const next = true; private static EventDispatcherInterface $dispatcher; diff --git a/src/Util/I18n.php b/src/Core/I18n.php similarity index 99% rename from src/Util/I18n.php rename to src/Core/I18n.php index 7be0454c77..0cf564b606 100644 --- a/src/Util/I18n.php +++ b/src/Core/I18n.php @@ -32,7 +32,7 @@ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -namespace App\Util; +namespace App\Core; // Locale category constants are usually predefined, but may not be // on some systems such as Win32. diff --git a/src/Util/Log.php b/src/Core/Log.php similarity index 98% rename from src/Util/Log.php rename to src/Core/Log.php index 469027923a..88bd04cfaf 100644 --- a/src/Util/Log.php +++ b/src/Core/Log.php @@ -29,7 +29,7 @@ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -namespace App\Util; +namespace App\Core; use Psr\Log\LoggerInterface; @@ -44,6 +44,7 @@ class Log /** * Simple static wrappers around Monolog's functions + * * @param string $msg */ public static function emergency(string $msg): void diff --git a/src/Util/ModulesManager.php b/src/Core/ModulesManager.php similarity index 96% rename from src/Util/ModulesManager.php rename to src/Core/ModulesManager.php index 10a91ff67b..ef6a3efb15 100644 --- a/src/Util/ModulesManager.php +++ b/src/Core/ModulesManager.php @@ -31,10 +31,10 @@ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -namespace App\Util; +namespace App\Core; -use App\Util\GSEvent as Event; use Functional as F; +use GSEvent as Event; abstract class ModulesManager { @@ -54,7 +54,7 @@ abstract class ModulesManager // Register event handlers $methods = get_class_methods($class); - $events = F\select($methods, F\partial_right('App\Util\Common::startsWith', 'on')); + $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]); diff --git a/src/Util/RouteLoader.php b/src/Core/RouteLoader.php similarity index 99% rename from src/Util/RouteLoader.php rename to src/Core/RouteLoader.php index 717c13873a..3e82986044 100644 --- a/src/Util/RouteLoader.php +++ b/src/Core/RouteLoader.php @@ -30,7 +30,7 @@ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -namespace App\Util; +namespace App\Core; use Symfony\Component\Config\Loader\Loader; use Symfony\Component\Routing\Route; diff --git a/src/Util/SchemaDefDriver.php b/src/Core/SchemaDefDriver.php similarity index 99% rename from src/Util/SchemaDefDriver.php rename to src/Core/SchemaDefDriver.php index 18e03e9949..cf36c5263d 100644 --- a/src/Util/SchemaDefDriver.php +++ b/src/Core/SchemaDefDriver.php @@ -28,7 +28,7 @@ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -namespace App\Util; +namespace App\Core; use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver; diff --git a/src/DependencyInjection/Compiler/SchemaDefPass.php b/src/DependencyInjection/Compiler/SchemaDefPass.php index 1e898e8082..fa9a84e372 100644 --- a/src/DependencyInjection/Compiler/SchemaDefPass.php +++ b/src/DependencyInjection/Compiler/SchemaDefPass.php @@ -44,7 +44,7 @@ class SchemaDefPass implements CompilerPassInterface { $container->findDefinition('doctrine.orm.default_metadata_driver') ->addMethodCall('addDriver', - [new Reference('app.util.schemadef_driver'), 'App\\Entity'] + [new Reference('app.core.schemadef_driver'), 'App\\Entity'] ); } } diff --git a/src/Kernel.php b/src/Kernel.php index a474b1ce79..cda1ca0f97 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -31,6 +31,7 @@ namespace App; use App\DependencyInjection\Compiler\SchemaDefPass; + use const PHP_VERSION_ID; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\Config\Loader\LoaderInterface; diff --git a/src/Routes/Main.php b/src/Routes/Main.php index 7c1e92bf6f..77fcc5ea92 100644 --- a/src/Routes/Main.php +++ b/src/Routes/Main.php @@ -1,21 +1,21 @@ . - */ +// {{{ 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 . +// }}} /** * Define social's main routes @@ -31,7 +31,7 @@ namespace App\Routes; use App\Controller\NetworkPublic; -use App\Util\RouteLoader; +use App\Core\RouteLoader; abstract class Main { diff --git a/src/Util/Common.php b/src/Util/Common.php index 1720881bdd..2aebe0fe1a 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -1,21 +1,21 @@ . - */ +// {{{ 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 . +// }}} /** * Common utility functions @@ -30,11 +30,6 @@ namespace App\Util; -use const DIRECTORY_SEPARATOR; -use Functional as F; -use InvalidArgumentException; -use Symfony\Component\Config\Definition\Exception\Exception; - abstract class Common { /** @@ -50,152 +45,4 @@ abstract class Common // TODO: implement it x) return []; } - - /** - * Normalize path by converting \ to / - * - * @param string $path - * - * @return string - */ - public static function normalizePath(string $path): string - { - if (DIRECTORY_SEPARATOR !== '/') { - $path = strtr($path, DIRECTORY_SEPARATOR, '/'); - } - return $path; - } - - /** - * Get plugin name from it's path, or null if not a plugin - * - * @param string $path - * - * @return null|string - */ - public static function pluginFromPath(string $path): ?string - { - $plug = strpos($path, '/plugins/'); - if ($plug === false) { - return null; - } - $cut = $plug + strlen('/plugins/'); - $cut2 = strpos($path, '/', $cut); - if ($cut2) { - $final = substr($path, $cut, $cut2 - $cut); - } else { - // We might be running directly from the plugins dir? - // If so, there's no place to store locale info. - throw new Exception('The GNU social install dir seems to contain a piece named plugin'); - } - return $final; - } - - /** - * Check whether $haystack starts with $needle - * - * @param array|string $haystack if array, check that all strings start with $needle - * @param string $needle - * - * @return bool - */ - public static function startsWith($haystack, string $needle): bool - { - if (is_string($haystack)) { - $length = strlen($needle); - return substr($haystack, 0, $length) === $needle; - } - return F\every($haystack, - function ($haystack) use ($needle) { - return self::startsWith($haystack, $needle); - }); - } - - /** - * Check whether $haystack ends with $needle - * - * @param array|string $haystack if array, check that all strings end with $needle - * @param string $needle - * - * @return bool - */ - public static function endsWith($haystack, string $needle) - { - if (is_string($haystack)) { - $length = strlen($needle); - if ($length == 0) { - return true; - } - return substr($haystack, -$length) === $needle; - } - return F\every($haystack, - function ($haystack) use ($needle) { - return self::endsWith($haystack, $needle); - }); - } - - /** - * Call $func with only abs($count) arguments, taken either from the - * left or right depending on the sign - * - * @param callable $func - * @param int $count - * - * @return callable - */ - public static function arity(callable $func, int $count): callable - { - return function (...$args) use ($func, $count) { - if ($count > 0) { - return call_user_func_array($func, F\take_left($args, $count)); - } - return call_user_func_array($func, F\take_right($args, -$count)); - }; - } - - /** - * @param string $str - * - * @return string - */ - public static function camelCaseToSnakeCase(string $str): string - { - return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str)); - } - - /** - * @param string $str - * - * @return string - */ - public static function snakeCaseToCamelCase(string $str): string - { - return implode('', F\map(preg_split('/[\b_]/', $str), self::arity('ucfirst', 1))); - } - - /** - * Indent $in, a string or array, $level levels - * - * @param array|string $in - * @param int $level - * @param int $count - * - * @return string - */ - public static function indent($in, int $level = 1, int $count = 2): string - { - if (is_string($in)) { - return self::indent(explode("\n", $in), $level, $count); - } elseif (is_array($in)) { - $indent = str_repeat(' ', $count * $level); - return implode("\n", F\map(F\select($in, - self::arity(function ($s) { - return $s != ''; - }, 1)), - function ($val) use ($indent) { - return F\concat($indent . $val); - })); - } - throw new InvalidArgumentException('Common:indent first parameter must be either an array or a string. Input was: ' . $in); - } } diff --git a/src/Util/Formatting.php b/src/Util/Formatting.php new file mode 100644 index 0000000000..7c672562b7 --- /dev/null +++ b/src/Util/Formatting.php @@ -0,0 +1,169 @@ +. +// }}} + +/** + * String formatting utilities + * + * @package GNUsocial + * @category Util + * + * @author Hugo Sales + * @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\Util; + +use const DIRECTORY_SEPARATOR; +use Functional; +use Functional as F; +use InvalidArgumentException; +use Symfony\Component\Config\Definition\Exception\Exception; + +abstract class Formatting +{ + /** + * Normalize path by converting \ to / + * + * @param string $path + * + * @return string + */ + public static function normalizePath(string $path): string + { + if (DIRECTORY_SEPARATOR !== '/') { + $path = strtr($path, DIRECTORY_SEPARATOR, '/'); + } + return $path; + } + + /** + * Get plugin name from it's path, or null if not a plugin + * + * @param string $path + * + * @return null|string + */ + public static function pluginFromPath(string $path): ?string + { + $plug = strpos($path, '/plugins/'); + if ($plug === false) { + return null; + } + $cut = $plug + strlen('/plugins/'); + $cut2 = strpos($path, '/', $cut); + if ($cut2) { + $final = substr($path, $cut, $cut2 - $cut); + } else { + // We might be running directly from the plugins dir? + // If so, there's no place to store locale info. + throw new Exception('The GNU social install dir seems to contain a piece named plugin'); + } + return $final; + } + + /** + * Check whether $haystack starts with $needle + * + * @param array|string $haystack if array, check that all strings start with $needle + * @param string $needle + * + * @return bool + */ + public static function startsWith($haystack, string $needle): bool + { + if (is_string($haystack)) { + $length = strlen($needle); + return substr($haystack, 0, $length) === $needle; + } + return F\every($haystack, + function ($haystack) use ($needle) { + return self::startsWith($haystack, $needle); + }); + } + + /** + * Check whether $haystack ends with $needle + * + * @param array|string $haystack if array, check that all strings end with $needle + * @param string $needle + * + * @return bool + */ + public static function endsWith($haystack, string $needle) + { + if (is_string($haystack)) { + $length = strlen($needle); + if ($length == 0) { + return true; + } + return substr($haystack, -$length) === $needle; + } + return F\every($haystack, + function ($haystack) use ($needle) { + return self::endsWith($haystack, $needle); + }); + } + + /** + * @param string $str + * + * @return string + */ + public static function camelCaseToSnakeCase(string $str): string + { + return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str)); + } + + /** + * @param string $str + * + * @return string + */ + public static function snakeCaseToCamelCase(string $str): string + { + return implode('', F\map(preg_split('/[\b_]/', $str), Functional::arity('ucfirst', 1))); + } + + /** + * Indent $in, a string or array, $level levels + * + * @param array|string $in + * @param int $level + * @param int $count + * + * @return string + */ + public static function indent($in, int $level = 1, int $count = 2): string + { + if (is_string($in)) { + return self::indent(explode("\n", $in), $level, $count); + } elseif (is_array($in)) { + $indent = str_repeat(' ', $count * $level); + return implode("\n", F\map(F\select($in, + Functional::arity(function ($s) { + return $s != ''; + }, 1)), + function ($val) use ($indent) { + return F\concat($indent . $val); + })); + } + throw new InvalidArgumentException('Formatting::indent\'s first parameter must be either an array or a string. Input was: ' . $in); + } +} diff --git a/src/Util/Functional.php b/src/Util/Functional.php new file mode 100644 index 0000000000..3a525229c5 --- /dev/null +++ b/src/Util/Functional.php @@ -0,0 +1,55 @@ +. +// }}} + +/** + * Functional utilities + * + * @package GNUsocial + * @category Util + * + * @author Hugo Sales + * @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\Util; + +use Functional as F; + +abstract class Functional +{ + /** + * Call $func with only abs($count) arguments, taken either from the + * left or right depending on the sign + * + * @param callable $func + * @param int $count + * + * @return callable + */ + public static function arity(callable $func, int $count): callable + { + return function (...$args) use ($func, $count) { + if ($count > 0) { + return call_user_func_array($func, F\take_left($args, $count)); + } + return call_user_func_array($func, F\take_right($args, -$count)); + }; + } +} diff --git a/src/Util/HTML.php b/src/Util/HTML.php index 18a03d8438..53abbd097b 100644 --- a/src/Util/HTML.php +++ b/src/Util/HTML.php @@ -1,6 +1,7 @@ . // }}} -namespace App\Util; - -use Functional as F; - /** * HTML Abstraction * @@ -27,6 +24,11 @@ use Functional as F; * @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\Util; + +use Functional as F; + abstract class HTML { /**