From cef8d9c208336db1ecea0c6ceaafef2aa2a5a286 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Mon, 16 Sep 2024 16:25:55 -0500 Subject: [PATCH] Update PostCreateScript --- builder/PostCreateScript.php | 51 +++++++++++++++++++++++++++++++---- src/Util/FakeApiRequester.php | 12 +++++---- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/builder/PostCreateScript.php b/builder/PostCreateScript.php index 1b5f5fa..a09489f 100755 --- a/builder/PostCreateScript.php +++ b/builder/PostCreateScript.php @@ -2,6 +2,7 @@ namespace Builder; +use ByJG\AnyDataset\Db\Factory; use ByJG\JwtWrapper\JwtWrapper; use ByJG\Util\Uri; use Composer\Script\Event; @@ -123,17 +124,57 @@ public static function run(Event $event) $currentPhpVersion = PHP_MAJOR_VERSION . "." .PHP_MINOR_VERSION; + $validatePHPVersion = function ($arg) { + $validPHPVersions = ['8.1', '8.2', '8.3']; + if (in_array($arg, $validPHPVersions)) { + return $arg; + } + throw new \Exception('Only the PHP versions ' . implode(', ', $validPHPVersions) . ' are supported'); + }; + + $validateNamespace = function ($arg) { + if (empty($arg) || !preg_match('/^[A-Z][a-zA-Z0-9]*$/', $arg)) { + throw new \Exception('Namespace must be one word in CamelCase'); + } + return $arg; + }; + + $validateComposer = function ($arg) { + if (empty($arg) || !preg_match('/^[a-z0-9-]+\/[a-z0-9-]+$/', $arg)) { + throw new \Exception('Invalid Composer name'); + } + return $arg; + }; + + $validateURI = function ($arg) { + $uri = new Uri($arg); + if (empty($uri->getScheme())) { + throw new \Exception('Invalid URI'); + } + Factory::getRegisteredDrivers($uri->getScheme()); + return $arg; + }; + + $validateTimeZone = function ($arg) { + if (empty($arg) || !in_array($arg, timezone_identifiers_list())) { + throw new \Exception('Invalid Timezone'); + } + return $arg; + }; + + $maxRetries = 5; + $stdIo->write("========================================================"); $stdIo->write(" Setup Project"); $stdIo->write(" Answer the questions below"); $stdIo->write("========================================================"); $stdIo->write(""); $stdIo->write("Project Directory: " . $workdir); - $phpVersion = $stdIo->ask("PHP Version [$currentPhpVersion]: ", $currentPhpVersion); - $namespace = $stdIo->ask('Project namespace [MyRest]: ', 'MyRest'); - $composerName = $stdIo->ask('Composer name [me/myrest]: ', 'me/myrest'); - $mysqlConnection = $stdIo->ask('MySQL connection DEV [mysql://root:mysqlp455w0rd@mysql-container/mydb]: ', 'mysql://root:mysqlp455w0rd@mysql-container/mydb'); - $timezone = $stdIo->ask('Timezone [UTC]: ', 'UTC'); + $phpVersion = $stdIo->askAndValidate("PHP Version [$currentPhpVersion]: ", $validatePHPVersion, $maxRetries, $currentPhpVersion); + $namespace = $stdIo->askAndValidate('Project namespace [MyRest]: ', $validateNamespace, $maxRetries, 'MyRest'); + $composerName = $stdIo->askAndValidate('Composer name [me/myrest]: ', $validateComposer, $maxRetries, 'me/myrest'); + $mysqlConnection = $stdIo->askAndValidate('MySQL connection DEV [mysql://root:mysqlp455w0rd@mysql-container/mydb]: ', $validateURI, $maxRetries, 'mysql://root:mysqlp455w0rd@mysql-container/mydb'); + $timezone = $stdIo->askAndValidate('Timezone [UTC]: ', $validateTimeZone, $maxRetries, 'UTC'); $stdIo->ask('Press to continue'); $script = new PostCreateScript(); diff --git a/src/Util/FakeApiRequester.php b/src/Util/FakeApiRequester.php index 839dccb..f252db9 100644 --- a/src/Util/FakeApiRequester.php +++ b/src/Util/FakeApiRequester.php @@ -17,6 +17,7 @@ use ByJG\RestServer\Middleware\JwtMiddleware; use ByJG\RestServer\MockRequestHandler; use ByJG\RestServer\Route\OpenApiRouteList; +use ByJG\WebRequest\Exception\RequestException; use ByJG\WebRequest\MockClient; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -34,18 +35,19 @@ class FakeApiRequester extends AbstractRequester /** * @param RequestInterface $request * @return ResponseInterface + * @throws ClassNotFoundException * @throws ConfigException * @throws ConfigNotFoundException * @throws DependencyInjectionException - * @throws InvalidArgumentException - * @throws InvalidDateException - * @throws KeyNotFoundException - * @throws ReflectionException - * @throws ClassNotFoundException * @throws Error404Exception * @throws Error405Exception * @throws Error520Exception + * @throws InvalidArgumentException * @throws InvalidClassException + * @throws InvalidDateException + * @throws KeyNotFoundException + * @throws ReflectionException + * @throws RequestException */ protected function handleRequest(RequestInterface $request): ResponseInterface {