From da50f82a7f0912bc7b4b27804db80f459ae68bd4 Mon Sep 17 00:00:00 2001 From: bert-w Date: Wed, 8 May 2024 21:05:28 +0200 Subject: [PATCH] implement trait and higher order proxies --- src/Illuminate/Config/Repository.php | 24 ++++++---------- src/Illuminate/Console/Command.php | 22 ++------------- .../Http/Concerns/InteractsWithInput.php | 8 +++--- src/Illuminate/Http/Request.php | 14 ++-------- src/Illuminate/Routing/Route.php | 11 ++++---- .../HigherOrderStrongTypeableProxy.php | 28 +++++++++++++++++++ .../Support/HigherOrderTypeableProxy.php | 28 +++++++++++++++++++ .../Support/Traits/StrongTypeable.php | 18 ++++++++++++ src/Illuminate/Support/Traits/Typeable.php | 18 ++++++++++++ tests/Support/SupportTypeableTest.php | 22 +++++++-------- 10 files changed, 125 insertions(+), 68 deletions(-) create mode 100644 src/Illuminate/Support/HigherOrderStrongTypeableProxy.php create mode 100644 src/Illuminate/Support/HigherOrderTypeableProxy.php create mode 100644 src/Illuminate/Support/Traits/StrongTypeable.php create mode 100644 src/Illuminate/Support/Traits/Typeable.php diff --git a/src/Illuminate/Config/Repository.php b/src/Illuminate/Config/Repository.php index baf2656a6fc6..a3987b7addbb 100644 --- a/src/Illuminate/Config/Repository.php +++ b/src/Illuminate/Config/Repository.php @@ -5,12 +5,13 @@ use ArrayAccess; use Illuminate\Contracts\Config\Repository as ConfigContract; use Illuminate\Support\Arr; -use Illuminate\Support\StrongTypeable; use Illuminate\Support\Traits\Macroable; +use Illuminate\Support\Traits\StrongTypeable; class Repository implements ArrayAccess, ConfigContract { - use Macroable; + use Macroable, + StrongTypeable; /** * All of the configuration items. @@ -19,13 +20,6 @@ class Repository implements ArrayAccess, ConfigContract */ protected $items = []; - /** - * The strong typeable instance for retrieving configuration values. - * - * @var StrongTypeable - */ - protected StrongTypeable $typeable; - /** * Create a new configuration repository. * @@ -35,8 +29,6 @@ class Repository implements ArrayAccess, ConfigContract public function __construct(array $items = []) { $this->items = $items; - - $this->typeable = new StrongTypeable($this, 'get'); } /** @@ -96,7 +88,7 @@ public function getMany($keys) */ public function string(string $key, $default = null): string { - return $this->typeable->string($key, $default); + return $this->typed()->get->string($key, $default); } /** @@ -108,7 +100,7 @@ public function string(string $key, $default = null): string */ public function integer(string $key, $default = null): int { - return $this->typeable->integer($key, $default); + return $this->typed()->get->integer($key, $default); } /** @@ -120,7 +112,7 @@ public function integer(string $key, $default = null): int */ public function float(string $key, $default = null): float { - return $this->typeable->float($key, $default); + return $this->typed()->get->float($key, $default); } /** @@ -132,7 +124,7 @@ public function float(string $key, $default = null): float */ public function boolean(string $key, $default = null): bool { - return $this->typeable->boolean($key, $default); + return $this->typed()->get->boolean($key, $default); } /** @@ -144,7 +136,7 @@ public function boolean(string $key, $default = null): bool */ public function array(string $key, $default = null): array { - return $this->typeable->array($key, $default); + return $this->typed()->get->array($key, $default); } /** diff --git a/src/Illuminate/Console/Command.php b/src/Illuminate/Console/Command.php index 195bdc003b00..e8c3b7a7aa0d 100755 --- a/src/Illuminate/Console/Command.php +++ b/src/Illuminate/Console/Command.php @@ -5,7 +5,7 @@ use Illuminate\Console\View\Components\Factory; use Illuminate\Contracts\Console\Isolatable; use Illuminate\Support\Traits\Macroable; -use Illuminate\Support\Typeable; +use Illuminate\Support\Traits\Typeable; use Symfony\Component\Console\Command\Command as SymfonyCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -19,7 +19,8 @@ class Command extends SymfonyCommand Concerns\InteractsWithIO, Concerns\InteractsWithSignals, Concerns\PromptsForMissingInput, - Macroable; + Macroable, + Typeable; /** * The Laravel application instance. @@ -84,20 +85,6 @@ class Command extends SymfonyCommand */ protected $aliases; - /** - * The typeable instance for retrieving command arguments. - * - * @var Typeable - */ - public Typeable $argument; - - /** - * The typeable instance for retrieving command options. - * - * @var Typeable - */ - public Typeable $option; - /** * Create a new console command instance. * @@ -138,9 +125,6 @@ public function __construct() if ($this instanceof Isolatable) { $this->configureIsolation(); } - - $this->argument = new Typeable($this, 'argument'); - $this->option = new Typeable($this, 'option'); } /** diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index ca6d7175f0aa..01522335649b 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -328,7 +328,7 @@ public function str($key, $default = null) */ public function string($key, $default = null) { - return $this->typeable->string($key, $default); + return $this->typed()->input->string($key, $default); } /** @@ -342,7 +342,7 @@ public function string($key, $default = null) */ public function boolean($key = null, $default = false) { - return $this->typeable->boolean($key, $default); + return $this->typed()->input->boolean($key, $default); } /** @@ -354,7 +354,7 @@ public function boolean($key = null, $default = false) */ public function integer($key, $default = 0) { - return $this->typeable->integer($key, $default); + return $this->typed()->input->integer($key, $default); } /** @@ -366,7 +366,7 @@ public function integer($key, $default = 0) */ public function float($key, $default = 0.0) { - return $this->typeable->float($key, $default); + return $this->typed()->input->float($key, $default); } /** diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index 230d796e5fe8..1817a0d671f1 100644 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -6,6 +6,7 @@ use Closure; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Session\SymfonySessionDecorator; +use Illuminate\Support; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; @@ -27,13 +28,9 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess Concerns\InteractsWithContentTypes, Concerns\InteractsWithFlashData, Concerns\InteractsWithInput, + Support\Traits\Typeable, Macroable; - /** - * @var Typeable - */ - public Typeable $typeable; - /** * The decoded JSON content for the request. * @@ -62,13 +59,6 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess */ protected $routeResolver; - public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null) - { - parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content); - - $this->typeable = $this->typeable(); - } - /** * Create a new Illuminate HTTP request from server variables. * diff --git a/src/Illuminate/Routing/Route.php b/src/Illuminate/Routing/Route.php index 9706dff8ba51..05e6d4cbee16 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -17,14 +17,18 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; -use Illuminate\Support\Typeable; +use Illuminate\Support\Traits\Typeable; use Laravel\SerializableClosure\SerializableClosure; use LogicException; use Symfony\Component\Routing\Route as SymfonyRoute; class Route { - use CreatesRegularExpressionRouteConstraints, FiltersControllerMiddleware, Macroable, ResolvesRouteDependencies; + use CreatesRegularExpressionRouteConstraints, + FiltersControllerMiddleware, + Macroable, + ResolvesRouteDependencies, + Typeable; /** * The URI pattern the route responds to. @@ -131,8 +135,6 @@ class Route */ public $compiled; - public Typeable $typeable; - /** * The router instance used by the route. * @@ -174,7 +176,6 @@ public function __construct($methods, $uri, $action) $this->uri = $uri; $this->methods = (array) $methods; $this->action = Arr::except($this->parseAction($action), ['prefix']); - $this->typeable = new Typeable($this, 'parameter'); if (in_array('GET', $this->methods) && ! in_array('HEAD', $this->methods)) { $this->methods[] = 'HEAD'; diff --git a/src/Illuminate/Support/HigherOrderStrongTypeableProxy.php b/src/Illuminate/Support/HigherOrderStrongTypeableProxy.php new file mode 100644 index 000000000000..8b231f79a473 --- /dev/null +++ b/src/Illuminate/Support/HigherOrderStrongTypeableProxy.php @@ -0,0 +1,28 @@ +target, $key); + } +} diff --git a/src/Illuminate/Support/HigherOrderTypeableProxy.php b/src/Illuminate/Support/HigherOrderTypeableProxy.php new file mode 100644 index 000000000000..311058425a39 --- /dev/null +++ b/src/Illuminate/Support/HigherOrderTypeableProxy.php @@ -0,0 +1,28 @@ +target, $key); + } +} diff --git a/src/Illuminate/Support/Traits/StrongTypeable.php b/src/Illuminate/Support/Traits/StrongTypeable.php new file mode 100644 index 000000000000..5f926cef84b2 --- /dev/null +++ b/src/Illuminate/Support/Traits/StrongTypeable.php @@ -0,0 +1,18 @@ +bind($request); - $this->assertInstanceOf(Stringable::class, $route->typeable->string('userid')); - $this->assertEquals('1234', $route->typeable->string('userid')); + $this->assertInstanceOf(Stringable::class, $route->typed()->parameter->string('userid')); + $this->assertEquals('1234', $route->typed()->parameter->string('userid')); - $this->assertIsBool($boolean = $route->typeable->boolean('unexisting')); + $this->assertIsBool($boolean = $route->typed()->parameter->boolean('unexisting')); $this->assertFalse($boolean); - $this->assertIsInt($integer = $route->typeable->integer('userid')); + $this->assertIsInt($integer = $route->typed()->parameter->integer('userid')); $this->assertEquals(1234, $integer); - $this->assertIsFloat($float = $route->typeable->float('userid')); + $this->assertIsFloat($float = $route->typed()->parameter->float('userid')); $this->assertEquals(1234.0, $float); } @@ -75,19 +73,19 @@ public function testTypeableCommand(): void $command->run($input, $output); - $this->assertInstanceOf(Stringable::class, $string = $command->option->string('userid')); + $this->assertInstanceOf(Stringable::class, $string = $command->typed()->option->string('userid')); $this->assertEquals('4', $string); - $this->assertIsBool($boolean = $command->option->boolean('all')); + $this->assertIsBool($boolean = $command->typed()->option->boolean('all')); $this->assertTrue($boolean); - $this->assertIsInt($integer = $command->option->integer('userid')); + $this->assertIsInt($integer = $command->typed()->option->integer('userid')); $this->assertEquals(4, $integer); - $this->assertIsFloat($float = $command->option->float('userid')); + $this->assertIsFloat($float = $command->typed()->option->float('userid')); $this->assertEquals(4.0, $float); - $this->assertIsArray($array = $command->argument->array('theargument')); + $this->assertIsArray($array = $command->typed()->argument->array('theargument')); $this->assertSame(['first', 'second'], $array); } }