Skip to content

Commit

Permalink
Add element registry
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 12, 2024
1 parent 900bfed commit a09b965
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "simplesamlphp/xml-common",
"description": "A library with classes and utilities for handling XML structures.",
"type": "project",
"type": "simplesamlphp-xmlprovider",
"keywords": ["saml", "xml"],
"homepage": "http://simplesamlphp.org",
"license": "LGPL-2.1-or-later",
Expand Down Expand Up @@ -34,7 +34,8 @@
"ext-spl": "*",
"ext-xmlreader": "*",

"simplesamlphp/assert": "^1.2"
"simplesamlphp/assert": "^1.2",
"symfony/finder": "^6.4"
},
"require-dev": {
"simplesamlphp/simplesamlphp-test-framework": "^1.7"
Expand All @@ -47,7 +48,8 @@
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"simplesamlphp/composer-xmlprovider-installer": true
}
}
}
89 changes: 89 additions & 0 deletions src/Registry/AbstractElementRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Registry;

use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use Symfony\Component\Finder\Finder;

use function array_key_exists;
use function array_merge;
use function dirname;
use function implode;

abstract class AbstractElementRegistry
{
/** @var \SimpleSAML\XML\Registry\AbstractElementRegistry|null $instance */
protected static ?AbstractElementRegistry $instance = null;


/** @var array<string, string> */
protected array $registry = [];

final private function __construct()
{
// Initialize the registry with all the elements we know
$classesDir = dirname(__FILE__, 3) . '/vendor/simplesamlphp/composer-xmlprovider-installer/classes';

$finder = Finder::create()->files()->name('element.registry.*.php')->in($classesDir);
if ($finder->hasResults()) {
foreach ($finder as $file) {
$elements = include($file);
$this->registry = array_merge($this->registry, $elements);
}
}
}


public static function getInstance(): AbstractElementRegistry
{
if (self::$instance === null) {
self::$instance = new static();
}

return self::$instance;
}


/**
* Register a class that can process a certain XML-element.
*
* @param string $class The class name of a class extending AbstractElement.
*/
public function registerElementHandler(string $class): void
{
Assert::subclassOf($class, AbstractElement::class);
$className = AbstractElement::getClassName($class);
$key = ($class::NS === null) ? $className : implode(':', [$class::NS, $className]);
$this->registry[$key] = $class;
}


/**
* Search for a class that implements an $element in the given $namespace.
*
* Such classes must have been registered previously by calling registerElementHandler(), and they must
* extend \SimpleSAML\XML\AbstractElement.
*
* @param string|null $namespace The namespace URI for the given element.
* @param string $element The local name of the element.
*
* @return string|null The fully-qualified name of a class extending \SimpleSAML\XML\AbstractElement and
* implementing support for the given element, or null if no such class has been registered before.
*/
public function getElementHandler(?string $namespace, string $element): ?string
{
Assert::nullOrValidURI($namespace, InvalidDOMElementException::class);
Assert::validNCName($element, InvalidDOMElementException::class);

$key = ($namespace === null) ? $element : implode(':', [$namespace, $element]);
if (array_key_exists($key, $this->registry) === true) {
return $this->registry[$key];
}

return null;
}
}

0 comments on commit a09b965

Please sign in to comment.