Description
A somewhat common pattern seen among the PHP community is making final classes with one method __invoke
like so:
final class DoSomething {
public function __constructor(/* args */) {
// set args
}
public function __invoke(): void {
// do something
}
}
Semantically, this is equivalent to the following done with functions and closures:
function doSomething(/* args */) {
return function() use (/* args */): void {
// do something
};
}
I'd like to discuss ways in which we could make the latter (functional service) easier to use in a symfony application.
The former pattern I think emerged in PHP rather than the latter due to PSR-4 composer autoloading defaults and the fact that most autowired di frameworks (like SF DI) use the class name to wire dependencies.
However, using the function/closure definition is much more terse, and allows you to easily include multiple related services into one file.
Example
With SF's new php fluent container configuration, i've been able to make use of functional services relatively pain free with the help of a few helper functions and abusing the php lexer.
// src/services.php
namespace App;
const serviceA = serviceA::class;
function serviceA(callable $serviceB) { // $serviceB is autowired
return function() use ($serviceB) {}; // does stuff
}
const serviceB = serviceB::class;
function serviceB(Psr\Log\LoggerInterface $logger) {
return function(): int {}; // does stuff
}
// src/util.php
namespace App;
/**
* Registers a service that is a function which returns a closure. If a suffix is applied, the function name is suffixed with `.{suffix}`
*/
function fnService(AbstractServiceConfigurator $container, string $fnName, ?string $suffix = null, ?string $bindName = null): ServiceConfigurator {
$serviceId = $fnName . ($suffix === null ? '' : '.' .$suffix)
$container->set($serviceId, 'Closure')->factory($fnName);
if (!$bindName) {
$parts = explode('\\', $fnName);
$bindName = end($parts);
}
$container->bind($bindName, ref($serviceId);
}
// config/services.php
use App;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function App\fnService;
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
return function($configurator) {
$container = $configurator->services()
->defaults()->autowire()->autoconfigure()->private();
fnService($container, App\serviceA);
fnService($container, App\serviceB);
};
Desired Outcomes
I'd be real stoked to see if we could do something like $container->fn()
which is basically my implementation of fnService
.
I'm also very open to ideas of how we could possible make this even more robust.
As far as dealing with the pain of having to register non-psr4 files into the files
section of the composer.json, that can be mitigated with a composer plugin like: https://github.com/krakphp/php-inc.