Skip to content

Commit

Permalink
Merge 9f7f862 into 1ca2415
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Jul 3, 2018
2 parents 1ca2415 + 9f7f862 commit 004f0c8
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 63 deletions.
71 changes: 47 additions & 24 deletions src/SiteAliasFileDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@
*/
class SiteAliasFileDiscovery
{
protected $searchLocations = [];
protected $depth = '<= 1';
protected $searchLocations;
protected $locationFilter;
protected $depth;

public function __construct($searchLocations = [], $depth = '<= 1', $locationFilter = null)
{
$this->locationFilter = $locationFilter;
$this->searchLocations = $searchLocations;
$this->depth = $depth;
}

/**
* Add a location that alias files may be found.
Expand All @@ -48,6 +56,10 @@ public function searchLocations()
return $this->searchLocations;
}

public function locationFilter()
{
return $this->locationFilter;
}

/**
* Set the search depth for finding alias files
Expand All @@ -61,6 +73,31 @@ public function depth($depth)
return $this;
}

/**
* Only search for aliases that are in alias files stored in directories
* whose basename or key matches the specified location.
*/
public function filterByLocation($location)
{
if (empty($location)) {
return $this;
}

return new SiteAliasFileDiscovery($this->searchLocations(), $this->depth, $location);
}

/**
* Find an alias file SITENAME.site.yml in one
* of the specified search locations.
*
* @param string $siteName
* @return string[]
*/
public function find($siteName)
{
return $this->searchForAliasFiles("$siteName.site.yml");
}

/**
* Find an alias file SITENAME.site.yml in one
* of the specified search locations.
Expand All @@ -70,7 +107,7 @@ public function depth($depth)
*/
public function findSingleSiteAliasFile($siteName)
{
$matches = $this->searchForAliasFiles("$siteName.site.yml");
$matches = $this->find($siteName);
if (empty($matches)) {
return false;
}
Expand Down Expand Up @@ -135,28 +172,14 @@ protected function searchForAliasFiles($searchPattern)
$path = $file->getRealPath();
$result[] = $path;
}
return $result;
}

/**
* Return a list of all alias files with the specified extension.
*
* @param string $filenameExensions
* @return string[]
*/
protected function searchForAliasFilesKeyedByBasenamePrefix($filenameExensions)
{
if (empty($this->searchLocations)) {
return [];
}
$searchPattern = '*' . $filenameExensions;
$finder = $this->createFinder($searchPattern);
$result = [];
foreach ($finder as $file) {
$path = $file->getRealPath();
$key = $this->extractKey($file->getBasename(), $filenameExensions);
$result[$key] = $path;
// In theory we can use $finder->path() instead. That didn't work well,
// in practice, though; had trouble correctly escaping the path separators.
if (!empty($this->locationFilter)) {
$result = array_filter($result, function ($path) {
return SiteAliasName::locationFromPath($path) === $this->locationFilter;
});
}

return $result;
}

Expand Down
57 changes: 47 additions & 10 deletions src/SiteAliasFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ public function loadAll()
* Return a list of all available alias files. Does not include
* legacy files.
*
* @param string $location Only consider alias files in the specified location.
* @return string[]
*/
public function listAll()
public function listAll($location = '')
{
return $this->discovery()->findAllSingleAliasFiles();
return $this->discovery()->filterByLocation($location)->findAllSingleAliasFiles();
}

/**
Expand All @@ -165,13 +166,42 @@ public function listAll()
*/
public function loadMultiple($sitename)
{
if ($path = $this->discovery()->findSingleSiteAliasFile($sitename)) {
$result = [];
foreach ($this->discovery()->find($sitename) as $path) {
if ($siteData = $this->loadSiteDataFromPath($path)) {
$location = SiteAliasName::locationFromPath($path);
// Convert the raw array into a list of alias records.
return $this->createAliasRecordsFromSiteData($sitename, $siteData);
$result = array_merge(
$result,
$this->createAliasRecordsFromSiteData($sitename, $siteData, $location)
);
}
}
return false;
return $result;
}


/**
* Given a location, return all alias files located there.
*
* @param string $location The location to filter.
* @return AliasRecord[]
*/
public function loadLocation($location)
{
$result = [];
foreach ($this->listAll($location) as $path) {
if ($siteData = $this->loadSiteDataFromPath($path)) {
$location = SiteAliasName::locationFromPath($path);
$sitename = $this->siteNameFromPath($path);
// Convert the raw array into a list of alias records.
$result = array_merge(
$result,
$this->createAliasRecordsFromSiteData($sitename, $siteData, $location)
);
}
}
return $result;
}

/**
Expand All @@ -181,15 +211,15 @@ public function loadMultiple($sitename)
* @param $siteData An associative array of envrionment => site data
* @return AliasRecord[]
*/
protected function createAliasRecordsFromSiteData($sitename, $siteData)
protected function createAliasRecordsFromSiteData($sitename, $siteData, $location = '')
{
$result = [];
if (!is_array($siteData) || empty($siteData)) {
return $result;
}
foreach ($siteData as $envName => $data) {
if (is_array($data)) {
$aliasName = new SiteAliasName($sitename, $envName);
$aliasName = new SiteAliasName($sitename, $envName, $location);

$processor = new ConfigProcessor();
$oneRecord = $this->fetchAliasRecordFromSiteAliasData($aliasName, $processor, $siteData);
Expand Down Expand Up @@ -223,7 +253,8 @@ protected function storeAliasRecordInResut(&$result, AliasRecord $aliasRecord)

/**
* If the alias name is '@sitename', or if it is '@sitename.env', then
* look for a sitename.site.yml file that contains it.
* look for a sitename.site.yml file that contains it. We also handle
* '@location.sitename.env' here as well.
*
* @param SiteAliasName $aliasName
*
Expand All @@ -233,7 +264,9 @@ protected function loadSingleAliasFile(SiteAliasName $aliasName)
{
// Check to see if the appropriate sitename.alias.yml file can be
// found. Return if it cannot.
$path = $this->discovery()->findSingleSiteAliasFile($aliasName->sitename());
$path = $this->discovery()
->filterByLocation($aliasName->location())
->findSingleSiteAliasFile($aliasName->sitename());
if (!$path) {
return false;
}
Expand Down Expand Up @@ -265,6 +298,10 @@ protected function loadSingleSiteAliasFileAtPath($path)
protected function siteNameFromPath($path)
{
return $this->basenameWithoutExtension($path, '.site.yml');

// OR:
// $filename = basename($path);
// return preg_replace('#\..*##', '', $filename);
}

/**
Expand Down Expand Up @@ -404,7 +441,7 @@ protected function fetchAliasRecordFromSiteAliasData(SiteAliasName $aliasName, C
$processor->add($data[$env]);

// Export the combined data and create an AliasRecord object to manage it.
return new AliasRecord($processor->export($this->referenceData), '@' . $aliasName->sitename(), $env);
return new AliasRecord($processor->export($this->referenceData), '@' . $aliasName->sitenameWithLocation(), $env);
}

/**
Expand Down
22 changes: 17 additions & 5 deletions src/SiteAliasManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,23 @@ public function getMultiple($name)
return false;
}

// Trim off the '@' and load all that match
$result = $this->aliasLoader->loadMultiple(ltrim($name, '@'));
// Trim off the '@'
$trimmedName = ltrim($name, '@');

// If the provided name is a location, return all aliases there
$result = $this->aliasLoader->loadLocation($trimmedName);
if (!empty($result)) {
return $result;
}

// If the provided name is a site, return all environments
$result = $this->aliasLoader->loadMultiple($trimmedName);
if (!empty($result)) {
return $result;
}

// Special checking for @self
if ($name == '@self') {
if ($trimmedName == 'self') {
$self = $this->getSelf();
$result = array_merge(
['@self' => $self],
Expand All @@ -194,8 +206,8 @@ public function getMultiple($name)
*
* @return string[]
*/
public function listAllFilePaths()
public function listAllFilePaths($location = '')
{
return $this->aliasLoader->listAll();
return $this->aliasLoader->listAll($location);
}
}
Loading

0 comments on commit 004f0c8

Please sign in to comment.