-
Notifications
You must be signed in to change notification settings - Fork 14
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
Add event registration #69
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
} | ||
} |
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 | ||
*/ | ||
|
@@ -10,6 +13,7 @@ class SiteAliasManager implements SiteAliasManagerInterface, SiteAliasManagerIni | |
protected $selfSiteAlias; | ||
protected $specParser; | ||
protected $root = ''; | ||
protected $dispatcher; | ||
|
||
/** | ||
* Constructor for SiteAliasManager | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -97,7 +102,15 @@ public function searchLocations() | |
public function get($name) | ||
{ | ||
if (SiteAliasName::isAliasName($name)) { | ||
return $this->getAlias($name); | ||
$alias = $this->getAlias($name); | ||
if (!$alias) { | ||
$event = new AliasNotFoundEvent($name); | ||
$this->dispatcher->dispatch($event, AliasNotFoundEvent::NAME); | ||
if ($event->hasAlias()) { | ||
return $event->getAlias(); | ||
} | ||
} | ||
return $alias; | ||
} | ||
|
||
if ($this->specParser->validSiteSpec($name)) { | ||
|
@@ -212,4 +225,9 @@ public function listAllFilePaths($location = '') | |
{ | ||
return $this->aliasLoader->listAll($location); | ||
} | ||
|
||
public function addListener($hook, callable $callback) | ||
{ | ||
$this->dispatcher->addListener($hook, $callback); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure, I made it generic with the notion that there might be other event types in the future. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming things: I'd prefer to see this called something like AliasDiscoveryEvent, as AliasNotFoundEvent sounds like error reporting rather than a second-tier searching event.