|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace wireframe\ComposerInstaller; |
| 4 | + |
| 5 | +use Composer\Package\PackageInterface; |
| 6 | +use Composer\Installer\LibraryInstaller; |
| 7 | + |
| 8 | +/** |
| 9 | + * BaseInstaller class |
| 10 | + * |
| 11 | + * This is a base class for ProcessWire CMS/CMF module and site profile |
| 12 | + * installers. |
| 13 | + * |
| 14 | + * @author Teppo Koivula <teppo@wireframe-framework.com> |
| 15 | + * @license Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/ |
| 16 | + */ |
| 17 | +class BaseInstaller extends LibraryInstaller |
| 18 | +{ |
| 19 | + /** |
| 20 | + * {@inheritDoc} |
| 21 | + */ |
| 22 | + public function getInstallPath(PackageInterface $package) |
| 23 | + { |
| 24 | + return parent::getInstallPath($package); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * {@inheritDoc} |
| 29 | + */ |
| 30 | + public function supports($packageType) |
| 31 | + { |
| 32 | + return parent::supports($packageType); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the base path for current installer |
| 37 | + * |
| 38 | + * Default base path is provided as an argument. This can be overridden by |
| 39 | + * specifying pw-[type]-path extra argument in composer.json, where "type" |
| 40 | + * is either "module" or "site-profile". |
| 41 | + * |
| 42 | + * @param string $defaultBasePath Default base path. |
| 43 | + * @return string Base path. |
| 44 | + */ |
| 45 | + protected function getBasePath($defaultBasePath) { |
| 46 | + // get the extra configuration of the top-level package |
| 47 | + $extra = []; |
| 48 | + if ($rootPackage = $this->composer->getPackage()) { |
| 49 | + $extra = $rootPackage->getExtra(); |
| 50 | + } |
| 51 | + |
| 52 | + // use base path from configuration, otherwise fall back to default |
| 53 | + $class = basename(str_replace('\\', '/', static::class)); |
| 54 | + $type = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', substr($class, 0, strrpos($class, 'Installer')))); |
| 55 | + $basePath = $extra['pw-' . $type . '-path'] ?? $defaultBasePath; |
| 56 | + |
| 57 | + return $basePath; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the directory name from package name |
| 62 | + * |
| 63 | + * Default directory name can be overridden by specifying extra argument |
| 64 | + * "installer-name" in the composer.json of the package in question. |
| 65 | + * |
| 66 | + * @param PackageInterface $package |
| 67 | + * @return string Module or site profile name. |
| 68 | + */ |
| 69 | + protected function getName(PackageInterface $package) { |
| 70 | + // determine the directory name from its package name |
| 71 | + $name = explode('/', $package->getPrettyName())[1]; |
| 72 | + |
| 73 | + // allow overriding module directory name via composer.json |
| 74 | + $extra = $package->getExtra(); |
| 75 | + if (!empty($extra['installer-name'])) { |
| 76 | + $name = $extra['installer-name']; |
| 77 | + } |
| 78 | + |
| 79 | + return $name; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get the full path with base part and package directory name |
| 84 | + * |
| 85 | + * @param PackageInterface $package |
| 86 | + * @param string $defaultBasePath Default base path. |
| 87 | + * @return string Path. |
| 88 | + */ |
| 89 | + protected function getFullPath(PackageInterface $package, $defaultBasePath) { |
| 90 | + $basePath = $this->getBasePath($defaultBasePath); |
| 91 | + $name = $this->getName($package); |
| 92 | + $path = trim($basePath, '/') . '/' . $name; |
| 93 | + return $path; |
| 94 | + } |
| 95 | +} |
0 commit comments