diff --git a/composer.json b/composer.json index f6cc9b9..3ec5766 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "require": { "php": ">=7.4", "consolidation/config": "^1.2.1 || ^2", + "symfony/event-dispatcher":"^5|| ^6", "symfony/filesystem": "^5.4 || ^6", "symfony/finder": "^5 || ^6" }, diff --git a/composer.lock b/composer.lock index a1be500..1d688da 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9b7a7566012d952bbf17cfcb02fed52d", + "content-hash": "7504c986f47d2aa268d067df3d16e26a", "packages": [ { "name": "consolidation/config", @@ -361,16 +361,16 @@ }, { "name": "symfony/event-dispatcher", - "version": "v5.4.9", + "version": "v5.4.26", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "8e6ce1cc0279e3ff3c8ff0f43813bc88d21ca1bc" + "reference": "5dcc00e03413f05c1e7900090927bb7247cb0aac" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/8e6ce1cc0279e3ff3c8ff0f43813bc88d21ca1bc", - "reference": "8e6ce1cc0279e3ff3c8ff0f43813bc88d21ca1bc", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/5dcc00e03413f05c1e7900090927bb7247cb0aac", + "reference": "5dcc00e03413f05c1e7900090927bb7247cb0aac", "shasum": "" }, "require": { @@ -426,7 +426,7 @@ "description": "Provides tools that allow your application components to communicate with each other by dispatching events and listening to them", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/event-dispatcher/tree/v5.4.9" + "source": "https://github.com/symfony/event-dispatcher/tree/v5.4.26" }, "funding": [ { @@ -442,7 +442,7 @@ "type": "tidelift" } ], - "time": "2022-05-05T16:45:39+00:00" + "time": "2023-07-06T06:34:20+00:00" }, { "name": "symfony/event-dispatcher-contracts", @@ -817,16 +817,16 @@ }, { "name": "symfony/polyfill-php80", - "version": "v1.26.0", + "version": "v1.28.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "cfa0ae98841b9e461207c13ab093d76b0fa7bace" + "reference": "6caa57379c4aec19c0a12a38b59b26487dcfe4b5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/cfa0ae98841b9e461207c13ab093d76b0fa7bace", - "reference": "cfa0ae98841b9e461207c13ab093d76b0fa7bace", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/6caa57379c4aec19c0a12a38b59b26487dcfe4b5", + "reference": "6caa57379c4aec19c0a12a38b59b26487dcfe4b5", "shasum": "" }, "require": { @@ -835,7 +835,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.26-dev" + "dev-main": "1.28-dev" }, "thanks": { "name": "symfony/polyfill", @@ -880,7 +880,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.26.0" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.28.0" }, "funding": [ { @@ -896,7 +896,7 @@ "type": "tidelift" } ], - "time": "2022-05-10T07:21:04+00:00" + "time": "2023-01-26T09:26:14+00:00" } ], "packages-dev": [ diff --git a/src/Events/AliasNotFoundEvent.php b/src/Events/AliasNotFoundEvent.php new file mode 100644 index 0000000..190d44b --- /dev/null +++ b/src/Events/AliasNotFoundEvent.php @@ -0,0 +1,36 @@ +aliasName = $aliasName; + } + + public function setAlias(SiteAlias $alias) + { + $this->alias = $alias; + } + + public function hasAlias() + { + return $this->alias !== false; + } + + public function getAlias() + { + return $this->alias; + } +} diff --git a/src/SiteAliasManager.php b/src/SiteAliasManager.php index 4105a2b..fe7e906 100644 --- a/src/SiteAliasManager.php +++ b/src/SiteAliasManager.php @@ -1,6 +1,9 @@ aliasLoader = $aliasLoader ?: new SiteAliasFileLoader(); $this->specParser = new SiteSpecParser(); $this->selfSiteAlias = new SiteAlias(); + $this->dispatcher = new EventDispatcher(); $this->setRoot($root); } @@ -97,7 +102,16 @@ public function searchLocations() public function get($name) { if (SiteAliasName::isAliasName($name)) { - return $this->getAlias($name); + $alias = $this->getAlias($name); + if (!$alias) { + //TODO, call all registered listeners here - if none of them return an alias, we return false + $event = new AliasNotFoundEvent($name); + $this->dispatcher->dispatch($event, AliasNotFoundEvent::NAME); + if ($event->hasAlias()) { + return $event->getAlias(); + } + return false; + } } if ($this->specParser->validSiteSpec($name)) { @@ -212,4 +226,9 @@ public function listAllFilePaths($location = '') { return $this->aliasLoader->listAll($location); } + + public function addListener($hook, callable $callback) + { + $this->dispatcher->addListener($hook, $callback); + } } diff --git a/tests/SiteAliasManagerTest.php b/tests/SiteAliasManagerTest.php index d80df01..915c856 100644 --- a/tests/SiteAliasManagerTest.php +++ b/tests/SiteAliasManagerTest.php @@ -1,9 +1,12 @@ assertEquals('@other.single.dev,@other.single.other', implode(',', $allNames)); } + /** + * This tests registering a custom site alias resolver when sites are not found using the standard resolution methods + * + */ + public function testEventListener() + { + /* @var SiteAlias $alias */ + $alias = $this->manager->get('@nonexistent.me'); + $this->assertFalse($alias, false); + $this->manager->addListener( + AliasNotFoundEvent::NAME, + function (AliasNotFoundEvent $event) { + $event->setAlias(new SiteAlias(['test' => 'isset'])); + } + ); + + $alias = $this->manager->get('@nonexistent.site'); + $this->assertEquals('isset', $alias->get('test')); + } + /** * @covers \Consolidation\SiteAlias\SiteAlias::root() */