Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions docs/best-practices/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ covers most common use cases:
* Each class may or may not have a constructor.
* If the constructor has an optional argument, it will be omitted unless an
explicit [container configuration](#container-configuration) is used.
* If the constructor has a nullable argument, it will be given a `null` value
unless an explicit [container configuration](#container-configuration) is used.
* If the constructor references another class, it will load this class next.

This covers most common use cases where the request handler class uses a
Expand Down Expand Up @@ -325,27 +323,6 @@ some manual configuration like this:
]);


$app = new FrameworkX\App($container);

// …
```

=== "Nullable values"

```php title="public/index.php"
<?php

require __DIR__ . '/../vendor/autoload.php';

$container = new FrameworkX\Container([
Acme\Todo\UserController::class => function (?string $name) {
// example UserController class uses $name, defaults to null if not set
return new Acme\Todo\UserController($name ?? 'ACME');
},
'name' => 'Demo'
]);


$app = new FrameworkX\App($container);

// …
Expand Down Expand Up @@ -415,7 +392,7 @@ all uppercase in any factory function like this:
// Framework X also uses environment variables internally.
// You may explicitly configure this built-in functionality like this:
// 'X_LISTEN' => '0.0.0.0:8081'
// 'X_LISTEN' => fn(?string $PORT = '8080') => '0.0.0.0:' . $PORT
// 'X_LISTEN' => fn(string $PORT = '8080') => '0.0.0.0:' . $PORT
'X_LISTEN' => '127.0.0.1:8080'
]);

Expand Down
17 changes: 6 additions & 11 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,6 @@ private function loadParameter(\ReflectionParameter $parameter, int $depth, bool
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
if ($type->allowsNull()) {
return null;
}

throw new \Error(
self::parameterError($parameter, $for) . ' expects unsupported type ' . $type
Expand All @@ -321,14 +318,12 @@ private function loadParameter(\ReflectionParameter $parameter, int $depth, bool
);
}

// use default/nullable argument if not loadable as container variable or by type
if (!$type instanceof \ReflectionNamedType || $type->isBuiltin() || !\array_key_exists($type->getName(), $this->container)) {
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
if ($type !== null && $type->allowsNull() && $type->getName() !== 'mixed') {
return null;
}
// use default argument if not loadable as container variable or by type
if (
$parameter->isDefaultValueAvailable() &&
(!$type instanceof \ReflectionNamedType || $type->isBuiltin() || !\array_key_exists($type->getName(), $this->container))
) {
return $parameter->getDefaultValue();
}

// abort if required container variable is not defined or for any other primitive types (array etc.)
Expand Down
Loading