-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(package): add a package service to map metadata paths to package…
…/vendor paths
- Loading branch information
1 parent
5169163
commit 9ffeec1
Showing
4 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
packages/app/src/Charcoal/App/Service/PackageMapService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Charcoal\App\Service; | ||
|
||
use UnexpectedValueException; | ||
|
||
/** | ||
* Class PackageMapService | ||
*/ | ||
class PackageMapService | ||
{ | ||
const PACKAGES_DIRECTORY = '/vendor/charcoal/charcoal/packages/'; | ||
private bool $isMonoRepo; | ||
private string $basePath; | ||
|
||
/** | ||
* @param array $data The init data. | ||
*/ | ||
public function __construct(array $data) | ||
{ | ||
$this->isMonoRepo = \Composer\InstalledVersions::isInstalled('charcoal/charcoal'); | ||
$this->basePath = $data['basePath']; | ||
} | ||
|
||
/** | ||
* @param string|string[] $value The value(s) to map to package. | ||
* @return mixed | ||
*/ | ||
public function map($value) | ||
{ | ||
if (is_array($value)) { | ||
return array_map([$this, 'mapOne'], $value); | ||
} | ||
|
||
return $this->mapOne($value); | ||
} | ||
|
||
/** | ||
* Replaces placeholders (%package.key.path%) by their values in the config. | ||
* | ||
* @param string $value A value to resolve. | ||
* @throws UnexpectedValueException If the resolved value is not a string or number. | ||
* @return mixed | ||
*/ | ||
public function mapOne(string $value): string | ||
{ | ||
return preg_replace_callback('/%%|%package\.([^\.%\s]+)\.path%/', function ($match) use ($value) { | ||
// skip escaped %% | ||
if (!isset($match[1])) { | ||
return '%%'; | ||
} | ||
|
||
$package = $match[1]; | ||
|
||
$resolved = ($this->resolvePackagePath($package) ?? null); | ||
|
||
if (!is_string($resolved) && !is_numeric($resolved)) { | ||
$resolvedType = (is_object($resolved) ? get_class($resolved) : gettype($resolved)); | ||
|
||
throw new UnexpectedValueException(sprintf( | ||
'Invalid config parameter "%s" inside string value "%s"; '. | ||
'must be a string or number, received %s', | ||
$package, | ||
$value, | ||
$resolvedType | ||
)); | ||
} | ||
|
||
return $resolved; | ||
}, $value); | ||
} | ||
|
||
/** | ||
* @param string $package The package string identifier. | ||
* @return string|null | ||
*/ | ||
private function resolvePackagePath(string $package): ?string | ||
{ | ||
if ($this->isMonoRepo) { | ||
$directory = self::PACKAGES_DIRECTORY.$package; | ||
|
||
if (file_exists($this->basePath.$directory)) { | ||
return $directory; | ||
} | ||
} else { | ||
$packageName = 'charcoal/'.$package; | ||
|
||
if (\Composer\InstalledVersions::isInstalled($packageName)) { | ||
return \Composer\InstalledVersions::getInstallPath($packageName); | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/app/src/Charcoal/App/ServiceProvider/PackageServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Charcoal\App\ServiceProvider; | ||
|
||
use Charcoal\App\Service\PackageMapService; | ||
use Pimple\Container; | ||
|
||
/** | ||
* Class PackageServiceProvider | ||
*/ | ||
class PackageServiceProvider implements \Pimple\ServiceProviderInterface | ||
{ | ||
|
||
/** | ||
* @param Container $container Pimple DI container. | ||
* @return void | ||
*/ | ||
public function register(Container $container) | ||
{ | ||
$container['package/map'] = function (Container $container) { | ||
return new PackageMapService([ | ||
'basePath' => $container['config']['base_path'] | ||
]); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters