-
-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24276 from totten/master-basic-svc
Autoload services based on interfaces/annotations. Convert APIv4 services.
- Loading branch information
Showing
87 changed files
with
1,320 additions
and
188 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Civi\Api4\Service; | ||
|
||
use Civi\Core\Service\AutoDefinition; | ||
use Civi\Core\Service\AutoServiceInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* This provides transitional support for extensions that implemented 'SpecProviderInterface'. | ||
* Compare these contracts: | ||
* | ||
* - For v5.19-v5.53, an extension could create a class in '$EXT/Civi/Api4/Service/Spec/Provider' | ||
* which implements 'SpecProviderInterface'. This would be auto-registered as a Symfony service | ||
* and tagged with 'spec_provider'. | ||
* - For v5.54+, an extension can enable `scan-classes@1`. Any classes in `$EXT/Civi` or `$EXT/CRM` | ||
* will be scanned and registered, provided that they implement AutoServiceInterface and | ||
* enable `scan-classes@1`. | ||
* | ||
* The 5.54+ scanner supports more interfaces and more options. However, it won't necessarily detect | ||
* spec-providers from 5.19-5.53 (because they don't have `scan-classes@1` and they don't | ||
* implement `AutoServiceInterface`). | ||
*/ | ||
class LegacySpecScanner implements AutoServiceInterface { | ||
|
||
public static function buildContainer(ContainerBuilder $container): void { | ||
$classNames = static::findClasses('Civi\Api4\Service\Spec\Provider', $container); | ||
foreach ($classNames as $className) { | ||
$class = new \ReflectionClass($className); | ||
if ($class->implementsInterface(AutoServiceInterface::class)) { | ||
// This is already handled by the main scanner. | ||
continue; | ||
} | ||
$container->addResource(new \Symfony\Component\Config\Resource\FileResource($class->getFileName())); | ||
$name = $class->getName(); /* str_replace('\\', '_', $class->getName()); */ | ||
$definition = AutoDefinition::create($className)->addTag('internal'); | ||
$container->setDefinition($name, $definition); | ||
} | ||
} | ||
|
||
/** | ||
* Scan all extensions for files in a certain namespace. | ||
* | ||
* @param string $namespace | ||
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | ||
* @return array | ||
*/ | ||
protected static function findClasses($namespace, $container): array { | ||
$classes = []; | ||
|
||
$namespace = \CRM_Utils_File::addTrailingSlash($namespace, '\\'); | ||
$locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')], | ||
array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath') | ||
); | ||
foreach ($locations as $location) { | ||
$path = \CRM_Utils_File::addTrailingSlash(dirname($location)) . str_replace('\\', DIRECTORY_SEPARATOR, $namespace); | ||
if (!file_exists($path) || !is_dir($path)) { | ||
$resource = new \Symfony\Component\Config\Resource\FileExistenceResource($path); | ||
$container->addResource($resource); | ||
} | ||
else { | ||
$resource = new \Symfony\Component\Config\Resource\DirectoryResource($path, ';\.php$;'); | ||
$container->addResource($resource); | ||
foreach (glob("$path*.php") as $file) { | ||
$matches = []; | ||
preg_match('/(\w*)\.php$/', $file, $matches); | ||
$classes[] = $namespace . array_pop($matches); | ||
} | ||
} | ||
} | ||
|
||
return $classes; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.