From a1ba0eb47373656a6f19587526fbdfbe06c46b2a Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Fri, 13 Sep 2024 12:34:08 +0200 Subject: [PATCH] Add importFromFile-method --- src/Registry/ElementRegistry.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Registry/ElementRegistry.php b/src/Registry/ElementRegistry.php index 26e43e4..20ac195 100644 --- a/src/Registry/ElementRegistry.php +++ b/src/Registry/ElementRegistry.php @@ -7,6 +7,7 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\XML\AbstractElement; use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\XML\Exception\IOException; use Symfony\Component\Finder\Finder; use function array_merge_recursive; @@ -22,7 +23,7 @@ final class ElementRegistry private array $registry = []; - final private function __construct() + private function __construct() { // Initialize the registry with all the elements we know $classesDir = dirname(__FILE__, 6) . '/vendor/simplesamlphp/composer-xmlprovider-installer/classes'; @@ -31,14 +32,24 @@ final private function __construct() $finder = Finder::create()->files()->name('element.registry.*.php')->in($classesDir); if ($finder->hasResults()) { foreach ($finder as $file) { - $elements = include($file); - $this->registry = array_merge_recursive($this->registry, $elements); + $this->importFromFile($file); } } } } + public function importFromFile(string $file): void + { + if (file_exists($file) === true) { + $elements = include($file); + $this->registry = array_merge_recursive($this->registry, $elements); + } else { + throw new IOException('File not found.'); + } + } + + public static function getInstance(): ElementRegistry { if (self::$instance === null) {