[CORE][UTIL] Moved classes from util to core

And splitted up Common
This commit is contained in:
Hugo Sales 2020-05-11 18:15:08 +00:00 committed by Hugo Sales
parent f628665589
commit dd559402cd
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
16 changed files with 280 additions and 206 deletions

View File

@ -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'

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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.

View File

@ -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

View File

@ -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]);

View File

@ -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;

View File

@ -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;

View File

@ -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']
);
}
}

View File

@ -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;

View File

@ -1,21 +1,21 @@
<?php
/*
* 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/>.
*/
// {{{ 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/>.
// }}}
/**
* 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
{

View File

@ -1,21 +1,21 @@
<?php
/*
* 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/>.
*/
// {{{ 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/>.
// }}}
/**
* 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);
}
}

169
src/Util/Formatting.php Normal file
View File

@ -0,0 +1,169 @@
<?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/>.
// }}}
/**
* String formatting utilities
*
* @package GNUsocial
* @category Util
*
* @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\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);
}
}

55
src/Util/Functional.php Normal file
View File

@ -0,0 +1,55 @@
<?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/>.
// }}}
/**
* Functional utilities
*
* @package GNUsocial
* @category Util
*
* @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\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));
};
}
}

View File

@ -1,6 +1,7 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/soci
// 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
@ -16,10 +17,6 @@
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
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
{
/**