From 7a9934df1cdfb9679464b0133e8f5421389283b0 Mon Sep 17 00:00:00 2001 From: brain Date: Mon, 15 Aug 2022 11:57:12 +0000 Subject: [PATCH] Improved autogenerator, uses class autoloading to cut down on copy paste --- .gitignore | 1 + .../classes/Generator/CoroGenerator.php | 127 ++++ .../classes/Generator/SyncGenerator.php | 131 ++++ .../classes/StructGeneratorInterface.php | 75 +++ buildtools/composer.json | 17 + buildtools/make_coro_struct.php | 225 ------- .../{make_sync_struct.php => make_struct.php} | 89 +-- buildtools/vendor/autoload.php | 12 + buildtools/vendor/composer/ClassLoader.php | 572 ++++++++++++++++++ buildtools/vendor/composer/LICENSE | 21 + .../vendor/composer/autoload_classmap.php | 10 + .../vendor/composer/autoload_namespaces.php | 9 + buildtools/vendor/composer/autoload_psr4.php | 10 + buildtools/vendor/composer/autoload_real.php | 36 ++ .../vendor/composer/autoload_static.php | 36 ++ library/CMakeLists.txt | 4 +- src/dpp/cluster_sync_calls.cpp | 1 + 17 files changed, 1085 insertions(+), 291 deletions(-) create mode 100644 buildtools/classes/Generator/CoroGenerator.php create mode 100644 buildtools/classes/Generator/SyncGenerator.php create mode 100644 buildtools/classes/StructGeneratorInterface.php create mode 100644 buildtools/composer.json delete mode 100644 buildtools/make_coro_struct.php rename buildtools/{make_sync_struct.php => make_struct.php} (67%) create mode 100644 buildtools/vendor/autoload.php create mode 100644 buildtools/vendor/composer/ClassLoader.php create mode 100644 buildtools/vendor/composer/LICENSE create mode 100644 buildtools/vendor/composer/autoload_classmap.php create mode 100644 buildtools/vendor/composer/autoload_namespaces.php create mode 100644 buildtools/vendor/composer/autoload_psr4.php create mode 100644 buildtools/vendor/composer/autoload_real.php create mode 100644 buildtools/vendor/composer/autoload_static.php diff --git a/.gitignore b/.gitignore index 103b07d5dc..470d27d1cd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ config.json docs testdata/maxpower.pcm testdata/onandon.pcm +buildtools/composer.phar docs/doxygen_sqlite3.db src/build .vs diff --git a/buildtools/classes/Generator/CoroGenerator.php b/buildtools/classes/Generator/CoroGenerator.php new file mode 100644 index 0000000000..e09ac52efe --- /dev/null +++ b/buildtools/classes/Generator/CoroGenerator.php @@ -0,0 +1,127 @@ +generateHeaderStart() . << +#include +#include +#include + +namespace dpp { + + +EOT; + } + + /** + * @inheritDoc + */ + public function checkForChanges(): bool + { + /* Check if we need to re-generate by comparing modification times */ + $us = file_exists('include/dpp/cluster_coro_calls.h') ? filemtime('include/dpp/cluster_coro_calls.h') : 0; + $them = filemtime('include/dpp/cluster.h'); + $thist = filemtime('buildtools/make_coro_struct.php'); + if ($them <= $us && $thist <= $us) { + echo "-- No change required.\n"; + return false; + } + + echo "-- Autogenerating include/dpp/cluster_coro_calls.h\n"; + return true; + } + + /** + * @inheritDoc + */ + public function generateHeaderDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string + { + return "auto inline co_{$currentFunction}($noDefaults) {\n\treturn dpp::awaitable(this, [&] (auto cc) { this->$currentFunction($parameterNames cc); }); \n}\n\n"; + } + + /** + * @inheritDoc + */ + public function generateCppDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string + { + return ''; + } + + /** + * @inheritDoc + */ + public function getCommentArray(): array + { + return [" * \memberof dpp::cluster"]; + } + + /** + * @inheritDoc + */ + public function saveHeader(string $content): void + { + file_put_contents('include/dpp/cluster_coro_calls.h', $content); + } + + /** + * @inheritDoc + */ + public function saveCpp(string $cppcontent): void + { + /* No cpp file to save, code is all inline */ + } + +} \ No newline at end of file diff --git a/buildtools/classes/Generator/SyncGenerator.php b/buildtools/classes/Generator/SyncGenerator.php new file mode 100644 index 0000000000..f37093ae61 --- /dev/null +++ b/buildtools/classes/Generator/SyncGenerator.php @@ -0,0 +1,131 @@ +generateHeaderStart() . << +#include +#include + +namespace dpp { + + +EOT; + } + + /** + * @inheritDoc + */ + public function checkForChanges(): bool + { + /* Check if we need to re-generate by comparing modification times */ + $us = file_exists('include/dpp/cluster_sync_calls.h') ? filemtime('include/dpp/cluster_sync_calls.h') : 0; + $them = filemtime('include/dpp/cluster.h'); + $thist = filemtime('buildtools/make_sync_struct.php'); + if ($them <= $us && $thist <= $us) { + echo "-- No change required.\n"; + return false; + } + + echo "-- Autogenerating include/dpp/cluster_sync_calls.h\n"; + echo "-- Autogenerating src/dpp/cluster_sync_calls.cpp\n"; + return true; + } + + /** + * @inheritDoc + */ + public function generateHeaderDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string + { + return "$returnType {$currentFunction}_sync($parameters);\n\n"; + } + + /** + * @inheritDoc + */ + public function generateCppDef(string $returnType, string $currentFunction, string $parameters, string $noDefaults, string $parameterNames): string + { + return "$returnType cluster::{$currentFunction}_sync($noDefaults) {\n\treturn dpp::sync<$returnType>(this, &cluster::$currentFunction$parameterNames);\n}\n\n"; + } + + /** + * @inheritDoc + */ + public function getCommentArray(): array + { + return [ + " * \memberof dpp::cluster", + " * @throw dpp::rest_exception upon failure to execute REST function", + " * @warning This function is a blocking (synchronous) call and should only be used from within a separate thread.", + " * Avoid direct use of this function inside an event handler.", + ]; + } + + /** + * @inheritDoc + */ + public function saveHeader(string $content): void + { + file_put_contents('include/dpp/cluster_sync_calls.h', $content); + } + + /** + * @inheritDoc + */ + public function saveCpp(string $cppcontent): void + { + file_put_contents('src/dpp/cluster_sync_calls.cpp', $cppcontent); + } +} \ No newline at end of file diff --git a/buildtools/classes/StructGeneratorInterface.php b/buildtools/classes/StructGeneratorInterface.php new file mode 100644 index 0000000000..65c044484f --- /dev/null +++ b/buildtools/classes/StructGeneratorInterface.php @@ -0,0 +1,75 @@ + 'message', - 'guild_get_members' => 'guild_member_map', - 'guild_search_members' => 'guild_member_map', - 'message_create' => 'message', - 'message_edit' => 'message', -]; - -/* Get the contents of cluster.h into an array */ -$header = explode("\n", file_get_contents('include/dpp/cluster.h')); - -/* Finite state machine state constants */ -define('STATE_SEARCH_FOR_FUNCTION', 0); -define('STATE_IN_FUNCTION', 1); -define('STATE_END_OF_FUNCTION', 2); - -$state = STATE_SEARCH_FOR_FUNCTION; -$currentFunction = $parameters = $returnType = ''; -$content = << -#include -#include -#include - -namespace dpp { - -EOT; - -/* Check if we need to re-generate by comparing modification times */ -$us = file_exists('include/dpp/cluster_coro_calls.h') ? filemtime('include/dpp/cluster_coro_calls.h') : 0; -$them = filemtime('include/dpp/cluster.h'); -$thist = filemtime('buildtools/make_coro_struct.php'); -if ($them <= $us && $thist <= $us) { - echo "-- No change required.\n"; - exit(0); -} - -echo "-- Autogenerating include/dpp/cluster_coro_calls.h\n"; -/* echo "-- Autogenerating src/dpp/cluster_coro_calls.cpp\n"; */ - -/* Scan every line of the C++ source */ -foreach ($clustercpp as $cpp) { - /* Look for declaration of function body */ - if ($state == STATE_SEARCH_FOR_FUNCTION && - preg_match('/^\s*void\s+cluster::([^(]+)\s*\((.*)command_completion_event_t\s*callback\s*\)/', $cpp, $matches)) { - $currentFunction = $matches[1]; - $parameters = preg_replace('/,\s*$/', '', $matches[2]); - if (!in_array($currentFunction, $blacklist)) { - $state = STATE_IN_FUNCTION; - } - /* Scan function body */ - } elseif ($state == STATE_IN_FUNCTION) { - /* End of function */ - if (preg_match('/^\}\s*$/', $cpp)) { - $state = STATE_END_OF_FUNCTION; - /* look for the return type of the method */ - } elseif (preg_match('/rest_request<([^>]+)>/', $cpp, $matches)) { - /* rest_request */ - $returnType = $matches[1]; - } elseif (preg_match('/rest_request_list<([^>]+)>/', $cpp, $matches)) { - /* rest_request_list */ - $returnType = $matches[1] . '_map'; - } elseif (preg_match('/callback\(confirmation_callback_t\(\w+, ([^(]+)\(.*, \w+\)\)/', $cpp, $matches)) { - /* confirmation_callback_t */ - $returnType = $matches[1]; - } elseif (!empty($forcedReturn[$currentFunction])) { - /* Forced return type */ - $returnType = $forcedReturn[$currentFunction]; - } - } - /* Completed parsing of function body */ - if ($state == STATE_END_OF_FUNCTION && !empty($currentFunction) && !empty($returnType)) { - if (!in_array($currentFunction, $blacklist)) { - $parameterList = explode(',', $parameters); - $parameterNames = []; - foreach ($parameterList as $parameter) { - $parts = explode(' ', trim($parameter)); - $parameterNames[] = trim(preg_replace('/[\s\*\&]+/', '', $parts[count($parts) - 1])); - } - $content .= getComments($currentFunction, $returnType, $parameterNames) . "\n"; - $fullParameters = getFullParameters($currentFunction, $parameterNames); - $parameterNames = trim(join(', ', $parameterNames)); - if (!empty($parameterNames)) { - $parameterNames = $parameterNames . ', '; - } - $noDefaults = $parameters; - $parameters = !empty($fullParameters) ? $fullParameters : $parameters; - /* $content .= "auto inline {$currentFunction}_coro($parameters);\n\n"; */ - $content .= "auto inline co_{$currentFunction}($noDefaults) {\n\treturn dpp::awaitable(this, [&] (auto cc) { this->$currentFunction($parameterNames cc); }); \n}\n\n"; - } - $currentFunction = $parameters = $returnType = ''; - $state = STATE_SEARCH_FOR_FUNCTION; - } -} -$content .= << $line) { - if (preg_match('/^\s*void\s+' . $currentFunction . '\s*\(.*' . join('.*', $parameters) . '.*command_completion_event_t\s*callback\s*/', $line)) { - /* Backpeddle */ - $lineIndex = 1; - for ($n = $i; $n != 0; --$n, $lineIndex++) { - $header[$n] = preg_replace('/^\t+/', '', $header[$n]); - $header[$n] = preg_replace('/@see (.+?)$/', '@see dpp::cluster::' . $currentFunction . "\n * @see \\1", $header[$n]); - $header[$n] = preg_replace('/@param callback .*$/', '@return ' . $returnType . ' returned object on completion', $header[$n]); - if (preg_match('/\s*\* On success /i', $header[$n])) { - $header[$n] = ""; - } - if (preg_match('/\s*\/\*\*\s*$/', $header[$n])) { - $part = array_slice($header, $n, $lineIndex - 1); - array_splice($part, count($part) - 1, 0, - [ - " * \memberof dpp::cluster", - /* " * @throw dpp::rest_exception upon failure to execute REST function", */ - /* " * @warning This function is a blocking (synchronous) call and should only be used from within a separate thread.", */ - /* " * Avoid direct use of this function inside an event handler.", */ - ] - ); - return str_replace("\n\n", "\n", join("\n", $part)); - } - } - return ''; - } - } - return ''; -} - -/* Finished parsing, output autogenerated files */ -file_put_contents('include/dpp/cluster_coro_calls.h', $content); -/* file_put_contents('src/dpp/cluster_coro_calls.cpp', $cppcontent); */ diff --git a/buildtools/make_sync_struct.php b/buildtools/make_struct.php similarity index 67% rename from buildtools/make_sync_struct.php rename to buildtools/make_struct.php index 481df6a656..4bed8c8681 100644 --- a/buildtools/make_sync_struct.php +++ b/buildtools/make_struct.php @@ -1,5 +1,20 @@ generateHeaderStart(); +$cppcontent = $generator->generatecppStart(); -EOT; -$cppcontent = $content; -$cppcontent .= << -#include -#include - -namespace dpp { - -EOT; - -/* Check if we need to re-generate by comparing modification times */ -$us = file_exists('include/dpp/cluster_sync_calls.h') ? filemtime('include/dpp/cluster_sync_calls.h') : 0; -$them = filemtime('include/dpp/cluster.h'); -$thist = filemtime('buildtools/make_sync_struct.php'); -if ($them <= $us && $thist <= $us) { - echo "-- No change required.\n"; +if (!$generator->checkForChanges()) { exit(0); } -echo "-- Autogenerating include/dpp/cluster_sync_calls.h\n"; -echo "-- Autogenerating src/dpp/cluster_sync_calls.cpp\n"; - /* Scan every line of the C++ source */ foreach ($clustercpp as $cpp) { /* Look for declaration of function body */ @@ -129,7 +97,7 @@ $parts = explode(' ', trim($parameter)); $parameterNames[] = trim(preg_replace('/[\s\*\&]+/', '', $parts[count($parts) - 1])); } - $content .= getComments($currentFunction, $returnType, $parameterNames) . "\n"; + $content .= getComments($generator, $currentFunction, $returnType, $parameterNames) . "\n"; $fullParameters = getFullParameters($currentFunction, $parameterNames); $parameterNames = trim(join(', ', $parameterNames)); if (!empty($parameterNames)) { @@ -137,8 +105,8 @@ } $noDefaults = $parameters; $parameters = !empty($fullParameters) ? $fullParameters : $parameters; - $content .= "$returnType {$currentFunction}_sync($parameters);\n\n"; - $cppcontent .= "$returnType cluster::{$currentFunction}_sync($noDefaults) {\n\treturn dpp::sync<$returnType>(this, &cluster::$currentFunction$parameterNames);\n}\n\n"; + $content .= $generator->generateHeaderDef($returnType, $currentFunction, $parameters, $noDefaults, $parameterNames); + $cppcontent .= $generator->generateCppDef($returnType, $currentFunction, $parameters, $noDefaults, $parameterNames); } $currentFunction = $parameters = $returnType = ''; $state = STATE_SEARCH_FOR_FUNCTION; @@ -182,7 +150,7 @@ function getFullParameters(string $currentFunction, array $parameters): string * @param array $parameters Parameter names * @return string Comment block */ -function getComments(string $currentFunction, string $returnType, array $parameters): string +function getComments(StructGeneratorInterface $generator, string $currentFunction, string $returnType, array $parameters): string { global $header; /* First find the function */ @@ -199,14 +167,7 @@ function getComments(string $currentFunction, string $returnType, array $paramet } if (preg_match('/\s*\/\*\*\s*$/', $header[$n])) { $part = array_slice($header, $n, $lineIndex - 1); - array_splice($part, count($part) - 1, 0, - [ - " * \memberof dpp::cluster", - " * @throw dpp::rest_exception upon failure to execute REST function", - " * @warning This function is a blocking (synchronous) call and should only be used from within a separate thread.", - " * Avoid direct use of this function inside an event handler.", - ] - ); + array_splice($part, count($part) - 1, 0, $generator->getCommentArray()); return str_replace("\n\n", "\n", join("\n", $part)); } } @@ -217,5 +178,5 @@ function getComments(string $currentFunction, string $returnType, array $paramet } /* Finished parsing, output autogenerated files */ -file_put_contents('include/dpp/cluster_sync_calls.h', $content); -file_put_contents('src/dpp/cluster_sync_calls.cpp', $cppcontent); +$generator->saveHeader($content); +$generator->savecpp($cppcontent); diff --git a/buildtools/vendor/autoload.php b/buildtools/vendor/autoload.php new file mode 100644 index 0000000000..e413f90d4c --- /dev/null +++ b/buildtools/vendor/autoload.php @@ -0,0 +1,12 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Autoload; + +/** + * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. + * + * $loader = new \Composer\Autoload\ClassLoader(); + * + * // register classes with namespaces + * $loader->add('Symfony\Component', __DIR__.'/component'); + * $loader->add('Symfony', __DIR__.'/framework'); + * + * // activate the autoloader + * $loader->register(); + * + * // to enable searching the include path (eg. for PEAR packages) + * $loader->setUseIncludePath(true); + * + * In this example, if you try to use a class in the Symfony\Component + * namespace or one of its children (Symfony\Component\Console for instance), + * the autoloader will first look for the class under the component/ + * directory, and it will then fallback to the framework/ directory if not + * found before giving up. + * + * This class is loosely based on the Symfony UniversalClassLoader. + * + * @author Fabien Potencier + * @author Jordi Boggiano + * @see https://www.php-fig.org/psr/psr-0/ + * @see https://www.php-fig.org/psr/psr-4/ + */ +class ClassLoader +{ + /** @var ?string */ + private $vendorDir; + + // PSR-4 + /** + * @var array[] + * @psalm-var array> + */ + private $prefixLengthsPsr4 = array(); + /** + * @var array[] + * @psalm-var array> + */ + private $prefixDirsPsr4 = array(); + /** + * @var array[] + * @psalm-var array + */ + private $fallbackDirsPsr4 = array(); + + // PSR-0 + /** + * @var array[] + * @psalm-var array> + */ + private $prefixesPsr0 = array(); + /** + * @var array[] + * @psalm-var array + */ + private $fallbackDirsPsr0 = array(); + + /** @var bool */ + private $useIncludePath = false; + + /** + * @var string[] + * @psalm-var array + */ + private $classMap = array(); + + /** @var bool */ + private $classMapAuthoritative = false; + + /** + * @var bool[] + * @psalm-var array + */ + private $missingClasses = array(); + + /** @var ?string */ + private $apcuPrefix; + + /** + * @var self[] + */ + private static $registeredLoaders = array(); + + /** + * @param ?string $vendorDir + */ + public function __construct($vendorDir = null) + { + $this->vendorDir = $vendorDir; + } + + /** + * @return string[] + */ + public function getPrefixes() + { + if (!empty($this->prefixesPsr0)) { + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); + } + + return array(); + } + + /** + * @return array[] + * @psalm-return array> + */ + public function getPrefixesPsr4() + { + return $this->prefixDirsPsr4; + } + + /** + * @return array[] + * @psalm-return array + */ + public function getFallbackDirs() + { + return $this->fallbackDirsPsr0; + } + + /** + * @return array[] + * @psalm-return array + */ + public function getFallbackDirsPsr4() + { + return $this->fallbackDirsPsr4; + } + + /** + * @return string[] Array of classname => path + * @psalm-return array + */ + public function getClassMap() + { + return $this->classMap; + } + + /** + * @param string[] $classMap Class to filename map + * @psalm-param array $classMap + * + * @return void + */ + public function addClassMap(array $classMap) + { + if ($this->classMap) { + $this->classMap = array_merge($this->classMap, $classMap); + } else { + $this->classMap = $classMap; + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, either + * appending or prepending to the ones previously set for this prefix. + * + * @param string $prefix The prefix + * @param string[]|string $paths The PSR-0 root directories + * @param bool $prepend Whether to prepend the directories + * + * @return void + */ + public function add($prefix, $paths, $prepend = false) + { + if (!$prefix) { + if ($prepend) { + $this->fallbackDirsPsr0 = array_merge( + (array) $paths, + $this->fallbackDirsPsr0 + ); + } else { + $this->fallbackDirsPsr0 = array_merge( + $this->fallbackDirsPsr0, + (array) $paths + ); + } + + return; + } + + $first = $prefix[0]; + if (!isset($this->prefixesPsr0[$first][$prefix])) { + $this->prefixesPsr0[$first][$prefix] = (array) $paths; + + return; + } + if ($prepend) { + $this->prefixesPsr0[$first][$prefix] = array_merge( + (array) $paths, + $this->prefixesPsr0[$first][$prefix] + ); + } else { + $this->prefixesPsr0[$first][$prefix] = array_merge( + $this->prefixesPsr0[$first][$prefix], + (array) $paths + ); + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, either + * appending or prepending to the ones previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param string[]|string $paths The PSR-4 base directories + * @param bool $prepend Whether to prepend the directories + * + * @throws \InvalidArgumentException + * + * @return void + */ + public function addPsr4($prefix, $paths, $prepend = false) + { + if (!$prefix) { + // Register directories for the root namespace. + if ($prepend) { + $this->fallbackDirsPsr4 = array_merge( + (array) $paths, + $this->fallbackDirsPsr4 + ); + } else { + $this->fallbackDirsPsr4 = array_merge( + $this->fallbackDirsPsr4, + (array) $paths + ); + } + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { + // Register directories for a new namespace. + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = (array) $paths; + } elseif ($prepend) { + // Prepend directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + (array) $paths, + $this->prefixDirsPsr4[$prefix] + ); + } else { + // Append directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + $this->prefixDirsPsr4[$prefix], + (array) $paths + ); + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, + * replacing any others previously set for this prefix. + * + * @param string $prefix The prefix + * @param string[]|string $paths The PSR-0 base directories + * + * @return void + */ + public function set($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr0 = (array) $paths; + } else { + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, + * replacing any others previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param string[]|string $paths The PSR-4 base directories + * + * @throws \InvalidArgumentException + * + * @return void + */ + public function setPsr4($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr4 = (array) $paths; + } else { + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = (array) $paths; + } + } + + /** + * Turns on searching the include path for class files. + * + * @param bool $useIncludePath + * + * @return void + */ + public function setUseIncludePath($useIncludePath) + { + $this->useIncludePath = $useIncludePath; + } + + /** + * Can be used to check if the autoloader uses the include path to check + * for classes. + * + * @return bool + */ + public function getUseIncludePath() + { + return $this->useIncludePath; + } + + /** + * Turns off searching the prefix and fallback directories for classes + * that have not been registered with the class map. + * + * @param bool $classMapAuthoritative + * + * @return void + */ + public function setClassMapAuthoritative($classMapAuthoritative) + { + $this->classMapAuthoritative = $classMapAuthoritative; + } + + /** + * Should class lookup fail if not found in the current class map? + * + * @return bool + */ + public function isClassMapAuthoritative() + { + return $this->classMapAuthoritative; + } + + /** + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. + * + * @param string|null $apcuPrefix + * + * @return void + */ + public function setApcuPrefix($apcuPrefix) + { + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; + } + + /** + * The APCu prefix in use, or null if APCu caching is not enabled. + * + * @return string|null + */ + public function getApcuPrefix() + { + return $this->apcuPrefix; + } + + /** + * Registers this instance as an autoloader. + * + * @param bool $prepend Whether to prepend the autoloader or not + * + * @return void + */ + public function register($prepend = false) + { + spl_autoload_register(array($this, 'loadClass'), true, $prepend); + + if (null === $this->vendorDir) { + return; + } + + if ($prepend) { + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; + } else { + unset(self::$registeredLoaders[$this->vendorDir]); + self::$registeredLoaders[$this->vendorDir] = $this; + } + } + + /** + * Unregisters this instance as an autoloader. + * + * @return void + */ + public function unregister() + { + spl_autoload_unregister(array($this, 'loadClass')); + + if (null !== $this->vendorDir) { + unset(self::$registeredLoaders[$this->vendorDir]); + } + } + + /** + * Loads the given class or interface. + * + * @param string $class The name of the class + * @return true|null True if loaded, null otherwise + */ + public function loadClass($class) + { + if ($file = $this->findFile($class)) { + includeFile($file); + + return true; + } + + return null; + } + + /** + * Finds the path to the file where the class is defined. + * + * @param string $class The name of the class + * + * @return string|false The path if found, false otherwise + */ + public function findFile($class) + { + // class map lookup + if (isset($this->classMap[$class])) { + return $this->classMap[$class]; + } + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { + return false; + } + if (null !== $this->apcuPrefix) { + $file = apcu_fetch($this->apcuPrefix.$class, $hit); + if ($hit) { + return $file; + } + } + + $file = $this->findFileWithExtension($class, '.php'); + + // Search for Hack files if we are running on HHVM + if (false === $file && defined('HHVM_VERSION')) { + $file = $this->findFileWithExtension($class, '.hh'); + } + + if (null !== $this->apcuPrefix) { + apcu_add($this->apcuPrefix.$class, $file); + } + + if (false === $file) { + // Remember that this class does not exist. + $this->missingClasses[$class] = true; + } + + return $file; + } + + /** + * Returns the currently registered loaders indexed by their corresponding vendor directories. + * + * @return self[] + */ + public static function getRegisteredLoaders() + { + return self::$registeredLoaders; + } + + /** + * @param string $class + * @param string $ext + * @return string|false + */ + private function findFileWithExtension($class, $ext) + { + // PSR-4 lookup + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; + + $first = $class[0]; + if (isset($this->prefixLengthsPsr4[$first])) { + $subPath = $class; + while (false !== $lastPos = strrpos($subPath, '\\')) { + $subPath = substr($subPath, 0, $lastPos); + $search = $subPath . '\\'; + if (isset($this->prefixDirsPsr4[$search])) { + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); + foreach ($this->prefixDirsPsr4[$search] as $dir) { + if (file_exists($file = $dir . $pathEnd)) { + return $file; + } + } + } + } + } + + // PSR-4 fallback dirs + foreach ($this->fallbackDirsPsr4 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { + return $file; + } + } + + // PSR-0 lookup + if (false !== $pos = strrpos($class, '\\')) { + // namespaced class name + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); + } else { + // PEAR-like class name + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; + } + + if (isset($this->prefixesPsr0[$first])) { + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { + if (0 === strpos($class, $prefix)) { + foreach ($dirs as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + } + } + } + + // PSR-0 fallback dirs + foreach ($this->fallbackDirsPsr0 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + + // PSR-0 include paths. + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { + return $file; + } + + return false; + } +} + +/** + * Scope isolated include. + * + * Prevents access to $this/self from included files. + * + * @param string $file + * @return void + * @private + */ +function includeFile($file) +{ + include $file; +} diff --git a/buildtools/vendor/composer/LICENSE b/buildtools/vendor/composer/LICENSE new file mode 100644 index 0000000000..f27399a042 --- /dev/null +++ b/buildtools/vendor/composer/LICENSE @@ -0,0 +1,21 @@ + +Copyright (c) Nils Adermann, Jordi Boggiano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +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. + diff --git a/buildtools/vendor/composer/autoload_classmap.php b/buildtools/vendor/composer/autoload_classmap.php new file mode 100644 index 0000000000..0fb0a2c194 --- /dev/null +++ b/buildtools/vendor/composer/autoload_classmap.php @@ -0,0 +1,10 @@ + $vendorDir . '/composer/InstalledVersions.php', +); diff --git a/buildtools/vendor/composer/autoload_namespaces.php b/buildtools/vendor/composer/autoload_namespaces.php new file mode 100644 index 0000000000..15a2ff3ad6 --- /dev/null +++ b/buildtools/vendor/composer/autoload_namespaces.php @@ -0,0 +1,9 @@ + array($baseDir . '/classes'), +); diff --git a/buildtools/vendor/composer/autoload_real.php b/buildtools/vendor/composer/autoload_real.php new file mode 100644 index 0000000000..4e39f0edba --- /dev/null +++ b/buildtools/vendor/composer/autoload_real.php @@ -0,0 +1,36 @@ +register(true); + + return $loader; + } +} diff --git a/buildtools/vendor/composer/autoload_static.php b/buildtools/vendor/composer/autoload_static.php new file mode 100644 index 0000000000..e41049c0c4 --- /dev/null +++ b/buildtools/vendor/composer/autoload_static.php @@ -0,0 +1,36 @@ + + array ( + 'Dpp\\' => 4, + ), + ); + + public static $prefixDirsPsr4 = array ( + 'Dpp\\' => + array ( + 0 => __DIR__ . '/../..' . '/classes', + ), + ); + + public static $classMap = array ( + 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', + ); + + public static function getInitializer(ClassLoader $loader) + { + return \Closure::bind(function () use ($loader) { + $loader->prefixLengthsPsr4 = ComposerStaticInit0e8415491642f27914717986db49b1db::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit0e8415491642f27914717986db49b1db::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit0e8415491642f27914717986db49b1db::$classMap; + + }, null, ClassLoader::class); + } +} diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 125aa3d50d..f9ccd5b98c 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -71,7 +71,7 @@ if (UNIX) # target for rebuild of cluster::*_sync() functions execute_process( WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.." - COMMAND php buildtools/make_sync_struct.php + COMMAND php buildtools/make_struct.php "\\Dpp\\Generator\\SyncGenerator" ) set_source_files_properties( "${CMAKE_CURRENT_SOURCE_DIR}/../include/dpp/cluster_sync_calls.h" PROPERTIES GENERATED TRUE ) else() @@ -285,7 +285,7 @@ if(DPP_CORO) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20") target_compile_definitions(dpp PUBLIC DPP_CORO) execute_process(WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.." - COMMAND php buildtools/make_coro_struct.php) + COMMAND php buildtools/make_struct.php "\\Dpp\\Generator\\CoroGenerator") endif() if(WIN32 AND NOT MINGW) diff --git a/src/dpp/cluster_sync_calls.cpp b/src/dpp/cluster_sync_calls.cpp index 2e52fb7cb0..b28a905306 100644 --- a/src/dpp/cluster_sync_calls.cpp +++ b/src/dpp/cluster_sync_calls.cpp @@ -32,6 +32,7 @@ #include namespace dpp { + slashcommand_map cluster::global_bulk_command_create_sync(const std::vector &commands) { return dpp::sync(this, &cluster::global_bulk_command_create, commands); }