Skip to content

Instantly share code, notes, and snippets.

@texdc
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save texdc/9093869 to your computer and use it in GitHub Desktop.

Select an option

Save texdc/9093869 to your computer and use it in GitHub Desktop.
Abstract custom Composer installer - https://getcomposer.org/doc/articles/custom-installers.md
<?php
namespace My\Composer\Installer;
use Composer\Package\PackageInterface;
use Composer\Installer\LibraryInstaller;
use My\Composer\Installer\Exception\InvalidPackageName;
abstract class AbstractInstaller extends LibraryInstaller
{
/**
* @var string vendor/foo-
*/
protected $prefix;
/**
* @var string path/to/my/install/dir/
*/
protected $installPath;
/**
* {@inheritDoc}
* @throws InvalidPackageName
*/
public function getPackageBasePath(PackageInterface $package)
{
$prefixLength = strlen($this->prefix);
$packageName = $package->getPrettyName();
if ($this->prefix !== substr($packageName, 0, $prefixLength)) {
throw new InvalidPackageName($packageName, $this->prefix);
}
return $this->installPath . substr($packageName, $prefixLength);
}
}
{
"name": "vendor/foo-bar",
"type": "composer-plugin",
"license": "MIT",
"autoload": {
"psr-4": {"My\\Composer": "src/"}
},
"extra": {
"class": [
"My\\Composer\\Installer\\FooInstaller",
"My\\Composer\\Installer\\BarInstaller"
]
},
"require": {
"composer-plugin-api": "~1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment