Skip to content

feat: autoconfigure interfaces #55013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
@@ -176,6 +176,13 @@ class Container implements ArrayAccess, ContainerContract
*/
protected $afterResolvingAttributeCallbacks = [];

/**
* An array of the interfaces to AutoConfigure
*
* @var class-string[]
*/
protected $autoconfigure = [];

/**
* Define a contextual binding.
*
@@ -1693,4 +1700,12 @@ public function __set($key, $value)
{
$this[$key] = $value;
}

/**
* @param class-string $interface
*/
public function autoconfigure(string $interface): void
{
$this->autoconfigure[] = $interface;
}
}
59 changes: 59 additions & 0 deletions src/Illuminate/Container/Util.php
Original file line number Diff line number Diff line change
@@ -12,6 +12,11 @@
*/
class Util
{
/**
* @var array<string, array<class-string, \ReflectionClass>>
*/
private static array $localCache;

/**
* If the given value is not an array and not null, wrap it in one.
*
@@ -84,4 +89,58 @@ public static function getContextualAttributeFromDependency($dependency)
{
return $dependency->getAttributes(ContextualAttribute::class, ReflectionAttribute::IS_INSTANCEOF)[0] ?? null;
}

/**
* This has been extracted from API Platform
*
* @param string $directory
*
* @return array<class-string, \ReflectionClass>
*/
public static function getReflectionClassesFromDirectory(string $directory): array
{
$id = hash('xxh3', $directory);
if (isset(self::$localCache[$id])) {
return self::$localCache[$id];
}

$includedFiles = [];
$iterator = new \RegexIterator(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
\RecursiveIteratorIterator::LEAVES_ONLY
),
'/^.+\.php$/i',
\RecursiveRegexIterator::GET_MATCH
);

foreach ($iterator as $file) {
$sourceFile = $file[0];

if (!preg_match('(^phar:)i', (string) $sourceFile)) {
$sourceFile = realpath($sourceFile);
}

try {
require_once $sourceFile;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely going to be an issue if the discovered file is a blade template or a pest unit file. Still looking to improve that...

} catch (\Throwable) {
// invalid PHP file (example: missing parent class)
continue;
}

$includedFiles[$sourceFile] = true;
}

$declared = array_merge(get_declared_classes(), get_declared_interfaces());
$ret = [];
foreach ($declared as $className) {
$reflectionClass = new \ReflectionClass($className);
$sourceFile = $reflectionClass->getFileName();
if (isset($includedFiles[$sourceFile])) {
$ret[$className] = $reflectionClass;
}
}

return self::$localCache[$id] = $ret;
}
}
17 changes: 17 additions & 0 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
use Closure;
use Composer\Autoload\ClassLoader;
use Illuminate\Container\Container;
use Illuminate\Container\Util;
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Illuminate\Contracts\Foundation\CachesConfiguration;
@@ -224,6 +225,7 @@ public function __construct($basePath = null)
$this->registerBaseServiceProviders();
$this->registerCoreContainerAliases();
$this->registerLaravelCloudServices();
$this->registerAutoConfigurableServices();
}

/**
@@ -1716,4 +1718,19 @@ public function getNamespace()

throw new RuntimeException('Unable to detect application namespace.');
}

protected function registerAutoConfigurableServices(): void
{
$classes = Util::getReflectionClassesFromDirectory(app_path());
foreach ($this->autoconfigure as $interface) {
$services = [];
foreach ($classes as $className => $refl) {
if ($refl->implementsInterface($interface)) {
$services[] = $className;
}
}

$this->app->tag($services, $interface);
}
}
}