Skip to content

Commit

Permalink
Automatically PascalCase module directory name and attempt to resolve…
Browse files Browse the repository at this point in the history
… site/site/ issues when installing modules from site directory
  • Loading branch information
Teppo Koivula committed Jun 16, 2019
1 parent 0b4b40a commit 82415fd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.5] - 2019-06-16

### Changed
- Convert installed module directories automatically to Pascal Case.
- Modify base path when installing from the site directory to avoid nested site/site/ directories.

## [0.0.4] - 2019-06-10

### Fixed
Expand Down
8 changes: 7 additions & 1 deletion src/ComposerInstaller/BaseInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public function supports($packageType)
* @return string Base path.
*/
protected function getBasePath($defaultBasePath) {
// if we're already in the site directory, remove "site/" from the beginning
// of the default base path to avoid creating nested /site/site/ directories
if (basename(\getcwd()) === 'site' && substr($defaultBasePath, 0, 5) === 'site/') {
$defaultBasePath = substr($defaultBasePath, 5);
}

// get the extra configuration of the top-level package
$extra = [];
if ($rootPackage = $this->composer->getPackage()) {
Expand Down Expand Up @@ -70,7 +76,7 @@ protected function getBasePath($defaultBasePath) {
* "installer-name" in the composer.json of the package in question.
*
* @param PackageInterface $package
* @return string Module or site profile name.
* @return string Module or site profile directory name.
*/
protected function getName(PackageInterface $package) {
// determine the directory name from its package name
Expand Down
38 changes: 38 additions & 0 deletions src/ComposerInstaller/ModuleInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,42 @@ public function getInstallPath(PackageInterface $package)
{
return $this->getFullPath($package, static::BASE_PATH);
}

/**
* Get the directory name from package name
*
* Default directory name can be overridden by specifying extra argument
* "installer-name" in the composer.json of the package in question.
*
* @param PackageInterface $package
* @return string Module directory name.
*/
protected function getName(PackageInterface $package) {
// fetch initial directory name
$name = parent::getName($package);

// make sure that the directory name is in Pascal Case
$name = $this->pascalCase($name);

return $name;
}

/**
* Convert string to Pascal Case
*
* Typically used for ProcessWire module directory names, which are loosely
* expected to follow a PascalCase type naming convention.
*
* @param string $string
* @return string String in PascalCase
*/
protected function pascalCase($string) {
$replace_chars = [
'_',
'-',
'/',
' '
];
return str_replace($replace_chars, '', ucwords($string, implode($replace_chars)));
}
}
2 changes: 1 addition & 1 deletion src/ComposerInstaller/SiteProfileInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getInstallPath(PackageInterface $package)
* "installer-name" in the composer.json of the package in question.
*
* @param PackageInterface $package
* @return string Module or site profile name.
* @return string Site profile directory name.
*/
protected function getName(PackageInterface $package) {
// fetch initial directory name
Expand Down

0 comments on commit 82415fd

Please sign in to comment.