8 use Composer\Repository\InstalledRepositoryInterface;
9 use Composer\IO\IOInterface;
10 use Composer\Composer;
12 use Composer\Json\JsonFile;
13 use Composer\Json\JsonManipulator;
14 use Composer\Installer\LibraryInstaller;
15 use Composer\Installer\InstallerInterface;
16 use Composer\Package\PackageInterface;
22 class Installer extends LibraryInstaller implements InstallerInterface
119 parent::__construct($io, $composer,
$type);
120 $this->initializeVendorDir();
124 $extra = $composer->getPackage()->getExtra();
126 if (isset(
$extra[
'magento-root-dir']) || $rootDirInput = $this->defaultMagentoRootDir) {
128 if (isset($rootDirInput)) {
129 $extra[
'magento-root-dir'] = $rootDirInput;
132 $dir = rtrim(trim(
$extra[
'magento-root-dir']),
'/\\');
133 $this->magentoRootDir = new \SplFileInfo($dir);
134 if (!
is_dir($dir) && $io->askConfirmation(
'magento root dir "' . $dir .
'" missing! create now? [Y,n] ')) {
136 $io->write(
'magento root dir "' . $dir .
'" created');
140 $dir = $this->vendorDir .
"/$dir";
141 $this->magentoRootDir = new \SplFileInfo($dir);
145 if (isset(
$extra[
'modman-root-dir'])) {
147 $dir = rtrim(trim(
$extra[
'modman-root-dir']),
'/\\');
149 $dir = $this->vendorDir .
"/$dir";
152 throw new \ErrorException(
"modman root dir \"{$dir}\" is not valid");
154 $this->modmanRootDir = new \SplFileInfo($dir);
157 if (isset(
$extra[
'magento-deploystrategy'])) {
158 $this->_deployStrategy = (string)
$extra[
'magento-deploystrategy'];
159 if($this->_deployStrategy !==
"copy"){
160 $io->write(
"<warning>Warning: Magento 2 is not tested with \"{$this->_deployStrategy}\" deployment strategy. It may not function properly.</warning>");
164 if ((is_null($this->magentoRootDir) ||
false === $this->magentoRootDir->isDir())
165 && $this->_deployStrategy !=
'none' 167 $dir = $this->magentoRootDir instanceof \SplFileInfo ? $this->magentoRootDir->getPathname() :
'';
168 $io->write(
"<error>magento root dir \"{$dir}\" is not valid</error>",
true);
169 $io->write(
'<comment>You need to set an existing path for "magento-root-dir" in your composer.json</comment>',
true);
170 $io->write(
'<comment>For more information please read about the "Usage" in the README of the installer Package</comment>',
true);
171 throw new \ErrorException(
"magento root dir \"{$dir}\" is not valid");
174 if (isset(
$extra[
'magento-force'])) {
175 $this->isForced = (bool)
$extra[
'magento-force'];
178 if (
false !== getenv(
'MAGENTO_CLOUD_PROJECT')) {
182 if (isset(
$extra[
'magento-deploystrategy'])) {
186 if (!empty(
$extra[
'auto-append-gitignore'])) {
190 if (!empty(
$extra[
'path-mapping-translations'])) {
191 $this->_pathMappingTranslations = (array)
$extra[
'path-mapping-translations'];
222 if (!$this->magentoRootDir->isDir()) {
223 $magentoRootPath = $this->magentoRootDir->getPathname();
224 $pathParts = explode(DIRECTORY_SEPARATOR, $magentoRootPath);
225 $baseDir = explode(DIRECTORY_SEPARATOR, $this->vendorDir);
227 $pathParts = array_merge(
$baseDir, $pathParts);
229 foreach ($pathParts as $pathPart) {
230 $directoryPath .= $pathPart . DIRECTORY_SEPARATOR;
231 $this->filesystem->ensureDirectoryExists($directoryPath);
245 private function updateJsonExtra(
$extra, $io) {
247 $file = Factory::getComposerFile();
250 $io->write(
'<error>' . $file .
' could not be created.</error>');
255 $io->write(
'<error>' . $file .
' is not readable.</error>');
260 $io->write(
'<error>' . $file .
' is not writable.</error>');
265 $json =
new JsonFile($file);
266 $composer = $json->read();
269 $baseExtra = array_key_exists($extraKey, $composer) ? $composer[$extraKey] : array();
271 if (!$this->updateFileCleanly($json, $baseExtra,
$extra, $extraKey)) {
273 $baseExtra[$key] =
$value;
276 $composer[$extraKey] = $baseExtra;
277 $json->write($composer);
281 private function updateFileCleanly($json, array
$base, array $new, $rootKey) {
284 $manipulator =
new JsonManipulator(
$contents);
286 foreach ($new as $childKey => $childValue) {
287 if (!$manipulator->addLink($rootKey, $childKey, $childValue)) {
302 $this->_deployStrategy = $strategy;
314 if (
null === $strategy) {
317 $extra = $this->composer->getPackage()->getExtra();
318 if( isset(
$extra[
'magento-deploystrategy-overwrite']) ){
320 if( isset($moduleSpecificDeployStrategys[$package->getName()]) ){
321 $strategy = $moduleSpecificDeployStrategys[$package->getName()];
324 $moduleSpecificDeployIgnores = array();
325 if( isset(
$extra[
'magento-deploy-ignore']) ){
327 if( isset(
$extra[
'magento-deploy-ignore'][
"*"]) ){
328 $moduleSpecificDeployIgnores =
$extra[
'magento-deploy-ignore'][
"*"];
330 if( isset(
$extra[
'magento-deploy-ignore'][$package->getName()]) ){
331 $moduleSpecificDeployIgnores = array_merge(
332 $moduleSpecificDeployIgnores,
333 $extra[
'magento-deploy-ignore'][$package->getName()]
337 if($package->getType() ===
'magento-core'){
344 $impl = new \MagentoHackathon\Composer\Magento\Deploystrategy\Symlink($sourceDir, $targetDir);
347 $impl = new \MagentoHackathon\Composer\Magento\Deploystrategy\Link($sourceDir, $targetDir);
350 $impl = new \MagentoHackathon\Composer\Magento\Deploystrategy\None($sourceDir, $targetDir);
354 $impl = new \MagentoHackathon\Composer\Magento\Deploystrategy\Copy($sourceDir, $targetDir);
357 $impl->setIsForced($this->isForced);
358 $impl->setIgnoredMappings($moduleSpecificDeployIgnores);
381 $this->filesystem->ensureDirectoryExists($this->vendorDir);
392 $targetDir = realpath($this->magentoRootDir->getPathname());
402 public function install(InstalledRepositoryInterface
$repo, PackageInterface $package)
409 parent::install(
$repo, $package);
412 if (!$this->hasExtraMap($package)) {
417 $strategy->setMappings($this->
getParser($package)->getMappings());
418 $deployManagerEntry =
new Entry();
419 $deployManagerEntry->setPackageName($package->getName());
420 $deployManagerEntry->setDeployStrategy($strategy);
421 $this->deployManager->addPackage($deployManagerEntry);
435 $ignoreFile = $this->magentoRootDir->getPathname() .
'/.gitignore';
451 if(file_exists($ignoreFile)) {
452 $contents = file($ignoreFile, FILE_IGNORE_NEW_LINES);
458 $ignore = sprintf(
"/%s", $dest);
459 $ignore = str_replace(
'/./',
'/', $ignore);
460 $ignore = str_replace(
'//',
'/', $ignore);
461 $ignore = rtrim($ignore,
'/');
464 if( in_array($ignore, $ignoredMappings) ){
473 array_unshift(
$additions,
'#' . $package->getName());
478 if ($package->getType() ===
'magento-core') {
490 if (!$this->io->askConfirmation(
'<info>Are you sure you want to install the Magento core?</info><error>Attention: Your Magento root dir will be cleared in the process!</error> [<comment>Y,n</comment>] ',
true)) {
491 $this->io->write(
'Skipping core installation...');
499 $this->filesystem->removeDirectory($this->magentoRootDir->getPathname());
500 $this->filesystem->ensureDirectoryExists($this->magentoRootDir->getPathname());
512 foreach ($this->_magentoWritableDirs as $dir) {
513 if (!file_exists($this->
getTargetDir() . DIRECTORY_SEPARATOR . $dir)) {
531 'Failed to set permissions "%s" for directory "%s"', decoct($dirmode),
$path 534 $dh = opendir(
$path);
535 while (($file = readdir($dh)) !==
false) {
536 if ($file !=
'.' && $file !=
'..') {
537 $fullpath =
$path .
'/' . $file;
545 'Failed to set permissions "%s" for file "%s"', decoct($filemode),
$path 552 $ioInterface = $this->io;
554 $composer = $this->composer;
555 $installedRepo = $composer->getRepositoryManager()->getLocalRepository();
557 $dm = $composer->getDownloadManager();
558 $im = $composer->getInstallationManager();
563 $moduleInstaller = $im->getInstaller(
"magento-module");
565 foreach ($installedRepo->getPackages() as $package) {
567 if ($ioInterface->isVerbose()) {
568 $ioInterface->write($package->getName());
569 $ioInterface->write($package->getType());
572 if ($package->getType() !=
"magento-module") {
575 if ($ioInterface->isVerbose()) {
576 $ioInterface->write(
"package {$package->getName()} recognized");
579 $strategy = $moduleInstaller->getDeployStrategy($package);
580 if ($ioInterface->getOption(
'verbose')) {
581 $ioInterface->write(
"used " . get_class($strategy) .
" as deploy strategy");
583 $strategy->setMappings($moduleInstaller->getParser($package)->getMappings());
601 public function update(InstalledRepositoryInterface
$repo, PackageInterface $initial, PackageInterface
$target)
609 if ($this->hasExtraMap($initial)) {
611 $initialStrategy->setMappings($this->
getParser($initial)->getMappings());
613 $initialStrategy->clean();
614 }
catch (\ErrorException $e) {
615 if ($this->io->isDebug()) {
616 $this->io->write($e->getMessage());
624 if ($this->hasExtraMap(
$target)) {
626 $targetStrategy->setMappings($this->
getParser($target)->getMappings());
627 $deployManagerEntry =
new Entry();
628 $deployManagerEntry->setPackageName(
$target->getName());
629 $deployManagerEntry->setDeployStrategy($targetStrategy);
630 $this->deployManager->addPackage($deployManagerEntry);
637 if ($target->getType() ===
'magento-core') {
644 if (!$this->io->askConfirmation(
'<info>Are you sure you want to manipulate the Magento core installation</info> [<comment>Y,n</comment>]? ',
true)) {
645 $this->io->write(
'Skipping core update...');
649 $this->filesystem->ensureDirectoryExists($tmpDir);
651 $this->magentoRootDir = new \SplFileInfo($tmpDir);
656 $tmpDir = $this->magentoRootDir->getPathname();
658 $this->backupMagentoRootDir = new \SplFileInfo($backupDir);
660 $origRootDir = $this->originalMagentoRootDir->getPathName();
661 $this->filesystem->rename($origRootDir, $backupDir);
662 $this->filesystem->rename($tmpDir, $origRootDir);
670 $rootDir = $this->magentoRootDir->getPathname();
671 $backupDir = $this->backupMagentoRootDir->getPathname();
672 $persistentFolders = array(
'media',
'var');
673 copy($backupDir . DIRECTORY_SEPARATOR . $this->_magentoLocalXmlPath,
$rootDir . DIRECTORY_SEPARATOR . $this->_magentoLocalXmlPath);
674 foreach ($persistentFolders as $folder) {
675 $this->filesystem->removeDirectory(
$rootDir . DIRECTORY_SEPARATOR . $folder);
676 $this->filesystem->rename($backupDir . DIRECTORY_SEPARATOR . $folder,
$rootDir . DIRECTORY_SEPARATOR . $folder);
678 if ($this->io->ask(
'Remove root backup? [Y,n] ',
true)) {
679 $this->filesystem->removeDirectory($backupDir);
680 $this->io->write(
'Removed root backup!',
true);
682 $this->io->write(
'Skipping backup removal...',
true);
688 if (($targetDir = $this->
getTargetDir()) && !$this->noMaintenanceMode) {
691 $this->io->write(
"Adding magento maintenance flag...");
693 }
elseif (file_exists($flagPath)) {
694 $this->io->write(
"Removing magento maintenance flag...");
701 if (($targetDir = $this->
getTargetDir()) && !$this->keepMagentoCache) {
703 if ($this->filesystem->removeDirectory($magentoCachePath)) {
704 $this->io->write(
'Magento cache cleared');
715 public function uninstall(InstalledRepositoryInterface
$repo, PackageInterface $package)
718 if (!$this->hasExtraMap($package)) {
719 parent::uninstall(
$repo, $package);
724 $strategy->setMappings($this->
getParser($package)->getMappings());
727 }
catch (\ErrorException $e) {
728 if ($this->io->isDebug()) {
729 $this->io->write($e->getMessage());
733 parent::uninstall(
$repo, $package);
745 $extra = $package->getExtra();
746 $moduleSpecificMap = $this->composer->getPackage()->getExtra();
747 if( isset($moduleSpecificMap[
'magento-map-overwrite']) ){
749 if( isset($moduleSpecificMap[$package->getName()]) ){
750 $map = $moduleSpecificMap[$package->getName()];
767 throw new \ErrorException(
'Unable to find deploy strategy for module: no known mapping');
778 if (!is_null($this->modmanRootDir) &&
true === $this->modmanRootDir->isDir()) {
779 $targetDir = $package->getTargetDir();
781 list($vendor, $targetDir) = explode(
'/', $package->getPrettyName());
783 $installPath = $this->modmanRootDir .
'/' . $targetDir;
785 $installPath = parent::getInstallPath($package);
789 if (DIRECTORY_SEPARATOR !== $installPath[0] && $installPath[1] !==
':') {
790 $installPath = getcwd() .
"/$installPath";
799 foreach($array as $key=>
$value){
800 $arrayNew[strtolower($key)] =
$value;
812 public function annoy(IOInterface $io)
832 private function hasExtraMap(PackageInterface $package) {
833 $packageExtra = $package->getExtra();
834 if (isset($packageExtra[
'map'])) {
setConfig(ProjectConfig $config)
setDeployManager(DeployManager $deployManager)
setDeployStrategy($strategy)
const MAGENTO_ROOT_DIR_TMP_SUFFIX
elseif(isset( $params[ 'redirect_parent']))
getSourceDir(PackageInterface $package)
initializeMagentoRootDir()
const MAGENTO_REMOVE_DEV_FLAG
getDeployStrategy(PackageInterface $package, $strategy=null)
setPermissions($path, $dirmode, $filemode)
appendGitIgnore(PackageInterface $package, $ignoreFile)
getGitIgnoreFileLocation()
const MAGENTO_MAINTANANCE_FLAG
transformArrayKeysToLowerCase($array)
uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
toggleMagentoMaintenanceMode($active=false)
update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
getParser(PackageInterface $package)
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
$_pathMappingTranslations
__construct(IOInterface $io, Composer $composer, $type='magento-module')
install(InstalledRepositoryInterface $repo, PackageInterface $package)
const MAGENTO_ROOT_DIR_BACKUP_SUFFIX
getInstallPath(PackageInterface $package)
if( $_orders &&count( $_orders))( 'Orders') ?></caption >< thead >< tr >< th scopeforeach( $_orders as $_order)(__( 'Order #')) ?>" class $extra
cleanupPostUpdateMagentoCore()