Skip to content
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

First pass at adding event dispatching for sites not found #67

Closed
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
28 changes: 14 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/Events/AliasNotFoundEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Consolidation\SiteAlias\Events;

use Consolidation\SiteAlias\SiteAlias;
use Symfony\Contracts\EventDispatcher\Event;

class AliasNotFoundEvent extends Event
{

protected $aliasName;

protected $alias = false;

const NAME = 'alias-not-found';

public function __construct($aliasName)
{
$this->aliasName = $aliasName;
}

public function setAlias(SiteAlias $alias)
{
$this->alias = $alias;
}

public function hasAlias()
{
return $this->alias !== false;
}

public function getAlias()
{
return $this->alias;
}
}
21 changes: 20 additions & 1 deletion src/SiteAliasManager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Consolidation\SiteAlias;

use Consolidation\SiteAlias\Events\AliasNotFoundEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;

/**
* Site Alias manager
*/
Expand All @@ -10,6 +13,7 @@ class SiteAliasManager implements SiteAliasManagerInterface, SiteAliasManagerIni
protected $selfSiteAlias;
protected $specParser;
protected $root = '';
protected $dispatcher;

/**
* Constructor for SiteAliasManager
Expand All @@ -21,6 +25,7 @@ public function __construct($aliasLoader = null, $root = '')
$this->aliasLoader = $aliasLoader ?: new SiteAliasFileLoader();
$this->specParser = new SiteSpecParser();
$this->selfSiteAlias = new SiteAlias();
$this->dispatcher = new EventDispatcher();
$this->setRoot($root);
}

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -212,4 +226,9 @@ public function listAllFilePaths($location = '')
{
return $this->aliasLoader->listAll($location);
}

public function addListener($hook, callable $callback)
{
$this->dispatcher->addListener($hook, $callback);
}
}
23 changes: 23 additions & 0 deletions tests/SiteAliasManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
namespace Consolidation\SiteAlias;

use Consolidation\SiteAlias\Events\AliasNotFoundEvent;
use Consolidation\SiteAlias\Util\YamlDataFileLoader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Yaml\Yaml;
use Symfony\Contracts\EventDispatcher\Event;

class SiteAliasManagerTest extends TestCase
{
Expand Down Expand Up @@ -162,6 +165,26 @@ public function testGetMultiple()
$this->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()
*/
Expand Down
Loading