public function dump(Config $config, InstalledRepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
    {
        $this->eventDispatcher->dispatchScript(ScriptEvents::PRE_AUTOLOAD_DUMP, $this->devMode);
        $filesystem = new Filesystem();
        $filesystem->ensureDirectoryExists($config->get('vendor-dir'));
        $basePath = $filesystem->normalizePath(realpath(getcwd()));
        $vendorPath = $filesystem->normalizePath(realpath($config->get('vendor-dir')));
        $useGlobalIncludePath = (bool) $config->get('use-include-path');
        $prependAutoloader = $config->get('prepend-autoloader') === false ? 'false' : 'true';
        $targetDir = $vendorPath . '/' . $targetDir;
        $filesystem->ensureDirectoryExists($targetDir);
        $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
        $vendorPathCode52 = str_replace('__DIR__', 'dirname(__FILE__)', $vendorPathCode);
        $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);
        $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, $basePath, true);
        $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
        $namespacesFile = <<<EOF
<?php

// autoload_namespaces.php @generated by Composer

\$vendorDir = {$vendorPathCode52};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        $psr4File = <<<EOF
<?php

// autoload_psr4.php @generated by Composer

\$vendorDir = {$vendorPathCode52};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        // Collect information from all packages.
        $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getCanonicalPackages());
        $autoloads = $this->parseAutoloads($packageMap, $mainPackage);
        // Process the 'psr-0' base directories.
        foreach ($autoloads['psr-0'] as $namespace => $paths) {
            $exportedPaths = array();
            foreach ($paths as $path) {
                $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
            }
            $exportedPrefix = var_export($namespace, true);
            $namespacesFile .= "    {$exportedPrefix} => ";
            $namespacesFile .= "array(" . implode(', ', $exportedPaths) . "),\n";
        }
        $namespacesFile .= ");\n";
        // Process the 'psr-4' base directories.
        foreach ($autoloads['psr-4'] as $namespace => $paths) {
            $exportedPaths = array();
            foreach ($paths as $path) {
                $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
            }
            $exportedPrefix = var_export($namespace, true);
            $psr4File .= "    {$exportedPrefix} => ";
            $psr4File .= "array(" . implode(', ', $exportedPaths) . "),\n";
        }
        $psr4File .= ");\n";
        $classmapFile = <<<EOF
<?php

// autoload_classmap.php @generated by Composer

\$vendorDir = {$vendorPathCode52};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        // add custom psr-0 autoloading if the root package has a target dir
        $targetDirLoader = null;
        $mainAutoload = $mainPackage->getAutoload();
        if ($mainPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) {
            $levels = count(explode('/', $filesystem->normalizePath($mainPackage->getTargetDir())));
            $prefixes = implode(', ', array_map(function ($prefix) {
                return var_export($prefix, true);
            }, array_keys($mainAutoload['psr-0'])));
            $baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, $basePath, true);
            $targetDirLoader = <<<EOF

    public static function autoload(\$class)
    {
        \$dir = {$baseDirFromTargetDirCode} . '/';
        \$prefixes = array({$prefixes});
        foreach (\$prefixes as \$prefix) {
            if (0 !== strpos(\$class, \$prefix)) {
                continue;
            }
            \$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), {$levels})).'.php';
            if (!\$path = stream_resolve_include_path(\$path)) {
                return false;
            }
            require \$path;

            return true;
        }
    }

EOF;
        }
        // flatten array
        $classMap = array();
        if ($scanPsr0Packages) {
            // Scan the PSR-0/4 directories for class files, and add them to the class map
            foreach (array('psr-0', 'psr-4') as $psrType) {
                foreach ($autoloads[$psrType] as $namespace => $paths) {
                    foreach ($paths as $dir) {
                        $dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath . '/' . $dir);
                        if (!is_dir($dir)) {
                            continue;
                        }
                        $whitelist = sprintf('{%s/%s.+(?<!(?<!/)Test\\.php)$}', preg_quote($dir), $psrType === 'psr-0' && strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : '');
                        $namespaceFilter = $namespace === '' ? null : $namespace;
                        foreach (ClassMapGenerator::createMap($dir, $whitelist, $this->io, $namespaceFilter) as $class => $path) {
                            if (!isset($classMap[$class])) {
                                $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
                                $classMap[$class] = $path . ",\n";
                            }
                        }
                    }
                }
            }
        }
        foreach ($autoloads['classmap'] as $dir) {
            foreach (ClassMapGenerator::createMap($dir, null, $this->io) as $class => $path) {
                $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
                $classMap[$class] = $path . ",\n";
            }
        }
        ksort($classMap);
        foreach ($classMap as $class => $code) {
            $classmapFile .= '    ' . var_export($class, true) . ' => ' . $code;
        }
        $classmapFile .= ");\n";
        if (!$suffix) {
            $suffix = $config->get('autoloader-suffix') ?: md5(uniqid('', true));
        }
        file_put_contents($targetDir . '/autoload_namespaces.php', $namespacesFile);
        file_put_contents($targetDir . '/autoload_psr4.php', $psr4File);
        file_put_contents($targetDir . '/autoload_classmap.php', $classmapFile);
        if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
            file_put_contents($targetDir . '/include_paths.php', $includePathFile);
        }
        if ($includeFilesFile = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
            file_put_contents($targetDir . '/autoload_files.php', $includeFilesFile);
        }
        file_put_contents($vendorPath . '/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix));
        file_put_contents($targetDir . '/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFile, $targetDirLoader, (bool) $includeFilesFile, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader));
        // use stream_copy_to_stream instead of copy
        // to work around https://bugs.php.net/bug.php?id=64634
        $sourceLoader = fopen(__DIR__ . '/ClassLoader.php', 'r');
        $targetLoader = fopen($targetDir . '/ClassLoader.php', 'w+');
        stream_copy_to_stream($sourceLoader, $targetLoader);
        fclose($sourceLoader);
        fclose($targetLoader);
        unset($sourceLoader, $targetLoader);
        $this->eventDispatcher->dispatchScript(ScriptEvents::POST_AUTOLOAD_DUMP, $this->devMode);
    }
Example #2
0
 /**
  * Run installation (or update)
  *
  * @return int 0 on success or a positive error code on failure
  *
  * @throws \Exception
  */
 public function run()
 {
     gc_collect_cycles();
     gc_disable();
     if ($this->dryRun) {
         $this->verbose = true;
         $this->runScripts = false;
         $this->installationManager->addInstaller(new NoopInstaller());
         $this->mockLocalRepositories($this->repositoryManager);
     }
     // TODO remove this BC feature at some point
     // purge old require-dev packages to avoid conflicts with the new way of handling dev requirements
     $devRepo = new InstalledFilesystemRepository(new JsonFile($this->config->get('vendor-dir') . '/composer/installed_dev.json'));
     if ($devRepo->getPackages()) {
         $this->io->writeError('<warning>BC Notice: Removing old dev packages to migrate to the new require-dev handling.</warning>');
         foreach ($devRepo->getPackages() as $package) {
             if ($this->installationManager->isPackageInstalled($devRepo, $package)) {
                 $this->installationManager->uninstall($devRepo, new UninstallOperation($package));
             }
         }
         unlink($this->config->get('vendor-dir') . '/composer/installed_dev.json');
     }
     unset($devRepo, $package);
     // end BC
     if ($this->runScripts) {
         // dispatch pre event
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
     }
     $this->downloadManager->setPreferSource($this->preferSource);
     $this->downloadManager->setPreferDist($this->preferDist);
     // clone root package to have one in the installed repo that does not require anything
     // we don't want it to be uninstallable, but its requirements should not conflict
     // with the lock file for example
     $installedRootPackage = clone $this->package;
     $installedRootPackage->setRequires(array());
     $installedRootPackage->setDevRequires(array());
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $localRepo = $this->repositoryManager->getLocalRepository();
     $platformRepo = new PlatformRepository();
     $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo);
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     $aliases = $this->getRootAliases();
     $this->aliasPlatformPackages($platformRepo, $aliases);
     try {
         $this->suggestedPackages = array();
         $res = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode);
         if ($res !== 0) {
             return $res;
         }
     } catch (\Exception $e) {
         $this->installationManager->notifyInstalls();
         throw $e;
     }
     $this->installationManager->notifyInstalls();
     // output suggestions if we're in dev mode
     if ($this->devMode) {
         foreach ($this->suggestedPackages as $suggestion) {
             $target = $suggestion['target'];
             foreach ($installedRepo->getPackages() as $package) {
                 if (in_array($target, $package->getNames())) {
                     continue 2;
                 }
             }
             $this->io->writeError($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')');
         }
     }
     # Find abandoned packages and warn user
     foreach ($localRepo->getPackages() as $package) {
         if (!$package instanceof CompletePackage || !$package->isAbandoned()) {
             continue;
         }
         $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested';
         $this->io->writeError(sprintf("<error>Package %s is abandoned, you should avoid using it. %s.</error>", $package->getPrettyName(), $replacement));
     }
     if (!$this->dryRun) {
         // write lock
         if ($this->update || !$this->locker->isLocked()) {
             $localRepo->reload();
             // if this is not run in dev mode and the root has dev requires, the lock must
             // contain null to prevent dev installs from a non-dev lock
             $devPackages = $this->devMode || !$this->package->getDevRequires() ? array() : null;
             // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
             if ($this->devMode && $this->package->getDevRequires()) {
                 $policy = $this->createPolicy();
                 $pool = $this->createPool(true);
                 $pool->addRepository($installedRepo, $aliases);
                 // creating requirements request
                 $request = $this->createRequest($pool, $this->package, $platformRepo);
                 $request->updateAll();
                 foreach ($this->package->getRequires() as $link) {
                     $request->install($link->getTarget(), $link->getConstraint());
                 }
                 $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request);
                 $solver = new Solver($policy, $pool, $installedRepo);
                 $ops = $solver->solve($request, $this->ignorePlatformReqs);
                 $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request, $ops);
                 foreach ($ops as $op) {
                     if ($op->getJobType() === 'uninstall') {
                         $devPackages[] = $op->getPackage();
                     }
                 }
             }
             $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
             $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array();
             $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), (array) $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest);
             if ($updatedLock) {
                 $this->io->writeError('<info>Writing lock file</info>');
             }
         }
         if ($this->dumpAutoloader) {
             // write autoloader
             if ($this->optimizeAutoloader) {
                 $this->io->writeError('<info>Generating optimized autoload files</info>');
             } else {
                 $this->io->writeError('<info>Generating autoload files</info>');
             }
             $this->autoloadGenerator->setDevMode($this->devMode);
             $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
         }
         if ($this->runScripts) {
             // dispatch post event
             $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
             $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
         }
         $vendorDir = $this->config->get('vendor-dir');
         if (is_dir($vendorDir)) {
             touch($vendorDir);
         }
     }
     return 0;
 }
Example #3
0
 /**
  * Run installation (or update)
  *
  * @throws \Exception
  * @return int        0 on success or a positive error code on failure
  */
 public function run()
 {
     // Disable GC to save CPU cycles, as the dependency solver can create hundreds of thousands
     // of PHP objects, the GC can spend quite some time walking the tree of references looking
     // for stuff to collect while there is nothing to collect. This slows things down dramatically
     // and turning it off results in much better performance. Do not try this at home however.
     gc_collect_cycles();
     gc_disable();
     // Force update if there is no lock file present
     if (!$this->update && !$this->locker->isLocked()) {
         $this->update = true;
     }
     if ($this->dryRun) {
         $this->verbose = true;
         $this->runScripts = false;
         $this->installationManager->addInstaller(new NoopInstaller());
         $this->mockLocalRepositories($this->repositoryManager);
     }
     if ($this->runScripts) {
         // dispatch pre event
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
     }
     $this->downloadManager->setPreferSource($this->preferSource);
     $this->downloadManager->setPreferDist($this->preferDist);
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $localRepo = $this->repositoryManager->getLocalRepository();
     if ($this->update) {
         $platformOverrides = $this->config->get('platform') ?: array();
     } else {
         $platformOverrides = $this->locker->getPlatformOverrides();
     }
     $platformRepo = new PlatformRepository(array(), $platformOverrides);
     $installedRepo = $this->createInstalledRepo($localRepo, $platformRepo);
     $aliases = $this->getRootAliases();
     $this->aliasPlatformPackages($platformRepo, $aliases);
     if (!$this->suggestedPackagesReporter) {
         $this->suggestedPackagesReporter = new SuggestedPackagesReporter($this->io);
     }
     try {
         list($res, $devPackages) = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases);
         if ($res !== 0) {
             return $res;
         }
     } catch (\Exception $e) {
         if (!$this->dryRun) {
             $this->installationManager->notifyInstalls($this->io);
         }
         throw $e;
     }
     if (!$this->dryRun) {
         $this->installationManager->notifyInstalls($this->io);
     }
     // output suggestions if we're in dev mode
     if ($this->devMode && !$this->skipSuggest) {
         $this->suggestedPackagesReporter->output($installedRepo);
     }
     # Find abandoned packages and warn user
     foreach ($localRepo->getPackages() as $package) {
         if (!$package instanceof CompletePackage || !$package->isAbandoned()) {
             continue;
         }
         $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested';
         $this->io->writeError(sprintf("<warning>Package %s is abandoned, you should avoid using it. %s.</warning>", $package->getPrettyName(), $replacement));
     }
     if (!$this->dryRun) {
         // write lock
         if ($this->update) {
             $localRepo->reload();
             $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
             $platformDevReqs = $this->extractPlatformRequirements($this->package->getDevRequires());
             $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest, $this->config->get('platform') ?: array());
             if ($updatedLock) {
                 $this->io->writeError('<info>Writing lock file</info>');
             }
         }
         if ($this->dumpAutoloader) {
             // write autoloader
             if ($this->optimizeAutoloader) {
                 $this->io->writeError('<info>Generating optimized autoload files</info>');
             } else {
                 $this->io->writeError('<info>Generating autoload files</info>');
             }
             $this->autoloadGenerator->setDevMode($this->devMode);
             $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative);
             $this->autoloadGenerator->setRunScripts($this->runScripts);
             $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
         }
         if ($this->runScripts) {
             $devMode = (int) $this->devMode;
             putenv("COMPOSER_DEV_MODE={$devMode}");
             // dispatch post event
             $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
             $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
         }
         // force binaries re-generation in case they are missing
         foreach ($localRepo->getPackages() as $package) {
             $this->installationManager->ensureBinariesPresence($package);
         }
         $vendorDir = $this->config->get('vendor-dir');
         if (is_dir($vendorDir)) {
             // suppress errors as this fails sometimes on OSX for no apparent reason
             // see https://github.com/composer/composer/issues/4070#issuecomment-129792748
             @touch($vendorDir);
         }
     }
     // re-enable GC except on HHVM which triggers a warning here
     if (!defined('HHVM_VERSION')) {
         gc_enable();
     }
     return 0;
 }
Example #4
0
    public function dump(Config $config, InstalledRepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
    {
        if ($this->classMapAuthoritative) {
            // Force scanPsr0Packages when classmap is authoritative
            $scanPsr0Packages = true;
        }
        $this->eventDispatcher->dispatchScript(ScriptEvents::PRE_AUTOLOAD_DUMP, $this->devMode, array(), array('optimize' => (bool) $scanPsr0Packages));
        $filesystem = new Filesystem();
        $filesystem->ensureDirectoryExists($config->get('vendor-dir'));
        $basePath = $filesystem->normalizePath(realpath(getcwd()));
        $vendorPath = $filesystem->normalizePath(realpath($config->get('vendor-dir')));
        $useGlobalIncludePath = (bool) $config->get('use-include-path');
        $prependAutoloader = $config->get('prepend-autoloader') === false ? 'false' : 'true';
        $targetDir = $vendorPath . '/' . $targetDir;
        $filesystem->ensureDirectoryExists($targetDir);
        $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
        $vendorPathCode52 = str_replace('__DIR__', 'dirname(__FILE__)', $vendorPathCode);
        $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);
        $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, $basePath, true);
        $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
        $namespacesFile = <<<EOF
<?php

// autoload_namespaces.php @generated by Composer

\$vendorDir = {$vendorPathCode52};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        $psr4File = <<<EOF
<?php

// autoload_psr4.php @generated by Composer

\$vendorDir = {$vendorPathCode52};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        // Collect information from all packages.
        $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getCanonicalPackages());
        $autoloads = $this->parseAutoloads($packageMap, $mainPackage);
        // Process the 'psr-0' base directories.
        foreach ($autoloads['psr-0'] as $namespace => $paths) {
            $exportedPaths = array();
            foreach ($paths as $path) {
                $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
            }
            $exportedPrefix = var_export($namespace, true);
            $namespacesFile .= "    {$exportedPrefix} => ";
            $namespacesFile .= "array(" . implode(', ', $exportedPaths) . "),\n";
        }
        $namespacesFile .= ");\n";
        // Process the 'psr-4' base directories.
        foreach ($autoloads['psr-4'] as $namespace => $paths) {
            $exportedPaths = array();
            foreach ($paths as $path) {
                $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
            }
            $exportedPrefix = var_export($namespace, true);
            $psr4File .= "    {$exportedPrefix} => ";
            $psr4File .= "array(" . implode(', ', $exportedPaths) . "),\n";
        }
        $psr4File .= ");\n";
        $classmapFile = <<<EOF
<?php

// autoload_classmap.php @generated by Composer

\$vendorDir = {$vendorPathCode52};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        // add custom psr-0 autoloading if the root package has a target dir
        $targetDirLoader = null;
        $mainAutoload = $mainPackage->getAutoload();
        if ($mainPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) {
            $levels = count(explode('/', $filesystem->normalizePath($mainPackage->getTargetDir())));
            $prefixes = implode(', ', array_map(function ($prefix) {
                return var_export($prefix, true);
            }, array_keys($mainAutoload['psr-0'])));
            $baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, $basePath, true);
            $targetDirLoader = <<<EOF

    public static function autoload(\$class)
    {
        \$dir = {$baseDirFromTargetDirCode} . '/';
        \$prefixes = array({$prefixes});
        foreach (\$prefixes as \$prefix) {
            if (0 !== strpos(\$class, \$prefix)) {
                continue;
            }
            \$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), {$levels})).'.php';
            if (!\$path = stream_resolve_include_path(\$path)) {
                return false;
            }
            require \$path;

            return true;
        }
    }

EOF;
        }
        $blacklist = null;
        if (!empty($autoloads['exclude-from-classmap'])) {
            $blacklist = '{(' . implode('|', $autoloads['exclude-from-classmap']) . ')}';
        }
        // flatten array
        $classMap = array();
        if ($scanPsr0Packages) {
            $namespacesToScan = array();
            // Scan the PSR-0/4 directories for class files, and add them to the class map
            foreach (array('psr-0', 'psr-4') as $psrType) {
                foreach ($autoloads[$psrType] as $namespace => $paths) {
                    $namespacesToScan[$namespace][] = array('paths' => $paths, 'type' => $psrType);
                }
            }
            krsort($namespacesToScan);
            foreach ($namespacesToScan as $namespace => $groups) {
                foreach ($groups as $group) {
                    $psrType = $group['type'];
                    foreach ($group['paths'] as $dir) {
                        $dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath . '/' . $dir);
                        if (!is_dir($dir)) {
                            continue;
                        }
                        //                        $whitelist = sprintf(
                        //                            '{%s/%s.+$}',
                        //                            preg_quote($dir),
                        //                            ($psrType === 'psr-0' && strpos($namespace, '_') === false) ? preg_quote(strtr($namespace, '\\', '/')) : ''
                        //                        );
                        $namespaceFilter = $namespace === '' ? null : $namespace;
                        $classMap = $this->addClassMapCode($filesystem, $basePath, $vendorPath, $dir, $blacklist, $namespaceFilter, $classMap);
                    }
                }
            }
        }
        foreach ($autoloads['classmap'] as $dir) {
            $classMap = $this->addClassMapCode($filesystem, $basePath, $vendorPath, $dir, $blacklist, null, $classMap);
        }
        ksort($classMap);
        foreach ($classMap as $class => $code) {
            $classmapFile .= '    ' . var_export($class, true) . ' => ' . $code;
        }
        $classmapFile .= ");\n";
        if (!$suffix) {
            if (!$config->get('autoloader-suffix') && is_readable($vendorPath . '/autoload.php')) {
                $content = file_get_contents($vendorPath . '/autoload.php');
                if (preg_match('{ComposerAutoloaderInit([^:\\s]+)::}', $content, $match)) {
                    $suffix = $match[1];
                }
            }
            if (!$suffix) {
                $suffix = $config->get('autoloader-suffix') ?: md5(uniqid('', true));
            }
        }
        file_put_contents($targetDir . '/autoload_namespaces.php', $namespacesFile);
        file_put_contents($targetDir . '/autoload_psr4.php', $psr4File);
        file_put_contents($targetDir . '/autoload_classmap.php', $classmapFile);
        $includePathFilePath = $targetDir . '/include_paths.php';
        if ($includePathFileContents = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
            file_put_contents($includePathFilePath, $includePathFileContents);
        } elseif (file_exists($includePathFilePath)) {
            unlink($includePathFilePath);
        }
        $includeFilesFilePath = $targetDir . '/autoload_files.php';
        if ($includeFilesFileContents = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
            file_put_contents($includeFilesFilePath, $includeFilesFileContents);
        } elseif (file_exists($includeFilesFilePath)) {
            unlink($includeFilesFilePath);
        }
        file_put_contents($vendorPath . '/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix));
        file_put_contents($targetDir . '/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFileContents, $targetDirLoader, (bool) $includeFilesFileContents, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader));
        $this->safeCopy(__DIR__ . '/ClassLoader.php', $targetDir . '/ClassLoader.php');
        $this->safeCopy(__DIR__ . '/../../../LICENSE', $targetDir . '/LICENSE');
        $this->eventDispatcher->dispatchScript(ScriptEvents::POST_AUTOLOAD_DUMP, $this->devMode, array(), array('optimize' => (bool) $scanPsr0Packages));
    }
Example #5
0
 /**
  * Run installation (or update)
  *
  * @throws \Exception
  * @return int        0 on success or a positive error code on failure
  */
 public function run()
 {
     // Disable GC to save CPU cycles, as the dependency solver can create hundreds of thousands
     // of PHP objects, the GC can spend quite some time walking the tree of references looking
     // for stuff to collect while there is nothing to collect. This slows things down dramatically
     // and turning it off results in much better performance. Do not try this at home however.
     gc_collect_cycles();
     gc_disable();
     if ($this->dryRun) {
         $this->verbose = true;
         $this->runScripts = false;
         $this->installationManager->addInstaller(new NoopInstaller());
         $this->mockLocalRepositories($this->repositoryManager);
     }
     if ($this->runScripts) {
         // dispatch pre event
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
     }
     $this->downloadManager->setPreferSource($this->preferSource);
     $this->downloadManager->setPreferDist($this->preferDist);
     // clone root package to have one in the installed repo that does not require anything
     // we don't want it to be uninstallable, but its requirements should not conflict
     // with the lock file for example
     $installedRootPackage = clone $this->package;
     $installedRootPackage->setRequires(array());
     $installedRootPackage->setDevRequires(array());
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $localRepo = $this->repositoryManager->getLocalRepository();
     if (!$this->update && $this->locker->isLocked()) {
         $platformOverrides = $this->locker->getPlatformOverrides();
     } else {
         $platformOverrides = $this->config->get('platform') ?: array();
     }
     $platformRepo = new PlatformRepository(array(), $platformOverrides);
     $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo);
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     $aliases = $this->getRootAliases();
     $this->aliasPlatformPackages($platformRepo, $aliases);
     try {
         $this->suggestedPackages = array();
         $res = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode);
         if ($res !== 0) {
             return $res;
         }
     } catch (\Exception $e) {
         if (!$this->dryRun) {
             $this->installationManager->notifyInstalls($this->io);
         }
         throw $e;
     }
     if (!$this->dryRun) {
         $this->installationManager->notifyInstalls($this->io);
     }
     // output suggestions if we're in dev mode
     if ($this->devMode) {
         foreach ($this->suggestedPackages as $suggestion) {
             $target = $suggestion['target'];
             foreach ($installedRepo->getPackages() as $package) {
                 if (in_array($target, $package->getNames())) {
                     continue 2;
                 }
             }
             $this->io->writeError($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')');
         }
     }
     # Find abandoned packages and warn user
     foreach ($localRepo->getPackages() as $package) {
         if (!$package instanceof CompletePackage || !$package->isAbandoned()) {
             continue;
         }
         $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested';
         $this->io->writeError(sprintf("<warning>Package %s is abandoned, you should avoid using it. %s.</warning>", $package->getPrettyName(), $replacement));
     }
     if (!$this->dryRun) {
         // write lock
         if ($this->update || !$this->locker->isLocked()) {
             $localRepo->reload();
             // if this is not run in dev mode and the root has dev requires, the lock must
             // contain null to prevent dev installs from a non-dev lock
             $devPackages = $this->devMode || !$this->package->getDevRequires() ? array() : null;
             // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
             if ($this->devMode && $this->package->getDevRequires()) {
                 $policy = $this->createPolicy();
                 $pool = $this->createPool(true);
                 $pool->addRepository($installedRepo, $aliases);
                 // creating requirements request
                 $request = $this->createRequest($this->package, $platformRepo);
                 $request->updateAll();
                 foreach ($this->package->getRequires() as $link) {
                     $request->install($link->getTarget(), $link->getConstraint());
                 }
                 $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request);
                 $solver = new Solver($policy, $pool, $installedRepo);
                 $ops = $solver->solve($request, $this->ignorePlatformReqs);
                 $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request, $ops);
                 foreach ($ops as $op) {
                     if ($op->getJobType() === 'uninstall') {
                         $devPackages[] = $op->getPackage();
                     }
                 }
             }
             $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
             $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array();
             $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), (array) $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest, $this->config->get('platform') ?: array());
             if ($updatedLock) {
                 $this->io->writeError('<info>Writing lock file</info>');
             }
         }
         if ($this->dumpAutoloader) {
             // write autoloader
             if ($this->optimizeAutoloader) {
                 $this->io->writeError('<info>Generating optimized autoload files</info>');
             } else {
                 $this->io->writeError('<info>Generating autoload files</info>');
             }
             $this->autoloadGenerator->setDevMode($this->devMode);
             $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative);
             $this->autoloadGenerator->setRunScripts($this->runScripts);
             $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
         }
         if ($this->runScripts) {
             // dispatch post event
             $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
             $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
         }
         $vendorDir = $this->config->get('vendor-dir');
         if (is_dir($vendorDir)) {
             // suppress errors as this fails sometimes on OSX for no apparent reason
             // see https://github.com/composer/composer/issues/4070#issuecomment-129792748
             @touch($vendorDir);
         }
     }
     // re-enable GC except on HHVM which triggers a warning here
     if (!defined('HHVM_VERSION')) {
         gc_enable();
     }
     return 0;
 }