private function normalize(string $path) : string { if (!$this->filesystem->isAbsolutePath($path)) { $path = $this->basePath . '/' . $path; } return $this->filesystem->normalizePath($path); }
/** * Creates an instance of InstallationManager * * @param string $vendorDir Relative path to the vendor directory * @throws \InvalidArgumentException */ public function __construct($vendorDir = 'vendor') { $fs = new Filesystem(); if ($fs->isAbsolutePath($vendorDir)) { $basePath = getcwd(); $relativePath = $fs->findShortestPath($basePath . '/file', $vendorDir); if ($fs->isAbsolutePath($relativePath)) { throw new \InvalidArgumentException("Vendor dir ({$vendorDir}) must be accessible from the directory ({$basePath})."); } $this->vendorPath = $relativePath; } else { $this->vendorPath = rtrim($vendorDir, '/'); } }
/** * Helper to convert relative paths to absolute ones. * * @param string[] $paths * @return string[] */ protected function absolutePaths($paths) { $return = array(); foreach ($paths as $path) { if (!$this->filesystem->isAbsolutePath($path)) { $path = getcwd() . '/' . $path; } $return[] = $path; } return $return; }
protected function generatePackagetAlias(PackageInterface $package) { $fs = new Filesystem(); $autoload = $package->getAutoload(); $baseDir = $this->baseDir; $aliases = []; if (!empty($autoload['psr-0'])) { foreach ($autoload['psr-0'] as $name => $path) { $name = str_replace('\\', '/', trim($name, '\\')); if (!$fs->isAbsolutePath($path)) { $path = $baseDir . '/' . $path; } $path = $fs->normalizePath($path); if (strpos($path . '/', $baseDir . '/') === 0) { $aliases["@{$name}"] = '<base-dir>' . substr($path, strlen($baseDir)) . '/' . $name; } else { $aliases["@{$name}"] = $path . '/' . $name; } } } if (!empty($autoload['psr-4'])) { foreach ($autoload['psr-4'] as $name => $path) { $name = str_replace('\\', '/', trim($name, '\\')); if (!$fs->isAbsolutePath($path)) { $path = $baseDir . '/' . $path; } $path = $fs->normalizePath($path); if (strpos($path . '/', $baseDir . '/') === 0) { $aliases["@{$name}"] = '<base-dir>' . substr($path, strlen($baseDir)); } else { $aliases["@{$name}"] = $path; } } } return $aliases; }
/** * Iterate over all files in the given directory searching for classes * * @param \Iterator|string $path The path to search in or an iterator * @param string $blacklist Regex that matches against the file path that exclude from the classmap. * @param IOInterface $io IO object * @param string $namespace Optional namespace prefix to filter by * * @throws \RuntimeException When the path is neither an existing file nor directory * @return array A class map array */ public static function createMap($path, $blacklist = null, IOInterface $io = null, $namespace = null) { if (is_string($path)) { if (is_file($path)) { $path = array(new \SplFileInfo($path)); } elseif (is_dir($path)) { $path = Finder::create()->files()->followLinks()->name('/\\.(php|inc|hh)$/')->in($path); } else { throw new \RuntimeException('Could not scan for classes inside "' . $path . '" which does not appear to be a file nor a folder'); } } $map = array(); $filesystem = new Filesystem(); $cwd = getcwd(); foreach ($path as $file) { $filePath = $file->getPathname(); if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc', 'hh'))) { continue; } if (!$filesystem->isAbsolutePath($filePath)) { $filePath = $cwd . '/' . $filePath; $filePath = $filesystem->normalizePath($filePath); } else { $filePath = preg_replace('{[\\\\/]{2,}}', '/', $filePath); } if ($blacklist && preg_match($blacklist, strtr($filePath, '\\', '/'))) { continue; } $classes = self::findClasses($filePath); foreach ($classes as $class) { // skip classes not within the given namespace prefix if (null !== $namespace && 0 !== strpos($class, $namespace)) { continue; } if (!isset($map[$class])) { $map[$class] = $filePath; } elseif ($io && $map[$class] !== $filePath && !preg_match('{/(test|fixture|example|stub)s?/}i', strtr($map[$class] . ' ' . $filePath, '\\', '/'))) { $io->writeError('<warning>Warning: Ambiguous class resolution, "' . $class . '"' . ' was found in both "' . $map[$class] . '" and "' . $filePath . '", the first will be used.</warning>'); } } } return $map; }
protected function getPathCode(Filesystem $filesystem, $basePath, $vendorPath, $path) { if (!$filesystem->isAbsolutePath($path)) { $path = $basePath . '/' . $path; } $path = $filesystem->normalizePath($path); $baseDir = ''; if (strpos($path . '/', $vendorPath . '/') === 0) { $path = substr($path, strlen($vendorPath)); $baseDir = '$vendorDir'; if ($path !== false) { $baseDir .= " . "; } } else { $path = $filesystem->normalizePath($filesystem->findShortestPath($basePath, $path, true)); if (!$filesystem->isAbsolutePath($path)) { $baseDir = '$baseDir . '; $path = '/' . $path; } } if (preg_match('/\\.phar$/', $path)) { $baseDir = "'phar://' . " . $baseDir; } return $baseDir . ($path !== false ? var_export($path, true) : ""); }
protected function getPath(Filesystem $filesystem, $relVendorPath, $vendorPath, $path) { $path = strtr($path, '\\', '/'); if (!$filesystem->isAbsolutePath($path)) { if (strpos($path, $relVendorPath) === 0) { // path starts with vendor dir return $vendorPath . substr($path, strlen($relVendorPath)); } return strtr(getcwd(), '\\', '/') . '/' . $path; } return $path; }
/** * An absolute path (leading '/') is converted to a file:// url. * * @param string $url * * @return string */ protected static function normalizeUrl($url) { $fs = new Filesystem(); if ($fs->isAbsolutePath($url)) { return 'file://' . strtr($url, '\\', '/'); } return $url; }
public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) { $autoloadFile = <<<'EOF' <?php // autoload.php generated by Composer if (!class_exists('Composer\\Autoload\\ClassLoader', false)) { require __DIR__.'/ClassLoader.php'; } $__composer_autoload_init = function() { $loader = new \Composer\Autoload\ClassLoader(); $map = require __DIR__.'/autoload_namespaces.php'; foreach ($map as $namespace => $path) { $loader->add($namespace, $path); } $loader->register(); return $loader; }; return $__composer_autoload_init(); EOF; $filesystem = new Filesystem(); $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/'); $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath); $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $namespacesFile = <<<EOF <?php // autoload_namespace.php generated by Composer \$vendorDir = {$vendorDirCode}; return array( EOF; $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages()); $autoloads = $this->parseAutoloads($packageMap); $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $namespace => $paths) { $exportedPaths = array(); foreach ($paths as $path) { $path = strtr($path, '\\', '/'); $baseDir = ''; if (!$filesystem->isAbsolutePath($path)) { // vendor dir == working dir if (preg_match('{^(\\./?)?$}', $relVendorPath)) { $path = '/' . $path; $baseDir = '$vendorDir . '; } elseif (strpos($path, $relVendorPath) === 0) { // path starts with vendor dir $path = substr($path, strlen($relVendorPath)); $baseDir = '$vendorDir . '; } else { $path = '/' . $path; $baseDir = $appBaseDir . ' . '; } } elseif (strpos($path, $vendorPath) === 0) { $path = substr($path, strlen($vendorPath)); $baseDir = '$vendorDir . '; } $exportedPaths[] = $baseDir . var_export($path, true); } $exportedPrefix = var_export($namespace, true); $namespacesFile .= " {$exportedPrefix} => "; if (count($exportedPaths) > 1) { $namespacesFile .= "array(" . implode(', ', $exportedPaths) . "),\n"; } else { $namespacesFile .= $exportedPaths[0] . ",\n"; } } } $namespacesFile .= ");\n"; file_put_contents($targetDir . '/autoload.php', $autoloadFile); file_put_contents($targetDir . '/autoload_namespaces.php', $namespacesFile); copy(__DIR__ . '/ClassLoader.php', $targetDir . '/ClassLoader.php'); }
function setup() { $allModulesDir = $this->parameters->getAllModulesDirs(); $allPluginsDir = $this->parameters->getAllPluginsDirs(); $allModules = $this->parameters->getAllSingleModuleDirs(); if (!count($allModulesDir) && !count($allModules) && !count($allPluginsDir)) { // nothing to setup return; } $appDir = $this->parameters->getAppDir(); if (!$appDir) { throw new \Exception("No application directory is set in JelixParameters"); } $configDir = $this->parameters->getVarConfigDir(); $vendorDir = $this->parameters->getVendorDir(); $fs = new Filesystem(); // open the localconfig.ini.php file $localinifile = $configDir . 'localconfig.ini.php'; if (!file_exists($localinifile)) { if (!file_exists($configDir)) { throw new \Exception('Configuration directory "' . $configDir . '" for the app does not exist'); } file_put_contents($localinifile, "<" . "?php\n;die(''); ?" . ">\n\n"); } $ini = new IniModifier($localinifile); if (count($allModulesDir)) { $modulesPath = ''; foreach ($allModulesDir as $path) { $path = $fs->findShortestPath($appDir, $vendorDir . $path, true); if ($fs->isAbsolutePath($path)) { $modulesPath .= ',' . $path; } else { $modulesPath .= ',app:' . $path; } } $modulesPath = trim($modulesPath, ','); $ini->setValue('modulesPath', $modulesPath); } if (count($allPluginsDir)) { $pluginsPath = ''; foreach ($allPluginsDir as $path) { $path = $fs->findShortestPath($appDir, $vendorDir . $path, true); if ($fs->isAbsolutePath($path)) { $pluginsPath .= ',' . $path; } else { $pluginsPath .= ',app:' . $path; } } $pluginsPath = trim($pluginsPath, ','); $ini->setValue('pluginsPath', $pluginsPath); } if (count($allModules)) { // erase first all "<module>.path" keys foreach ($ini->getValues('modules') as $key => $val) { if (preg_match("/\\.path\$/", $key)) { $ini->removeValue($key, "modules"); } } foreach ($allModules as $path) { $path = $fs->normalizePath($path); $moduleName = basename($path); $path = $fs->findShortestPath($appDir, $vendorDir . $path, true); if (!$fs->isAbsolutePath($path)) { $path = 'app:' . $path; } $ini->setValue($moduleName . '.path', $path, 'modules'); } } $ini->save(); }
protected function getPathCode(Filesystem $filesystem, $relVendorPath, $vendorPath, $path) { $path = strtr($path, '\\', '/'); $baseDir = ''; if (!$filesystem->isAbsolutePath($path)) { if (strpos($path, $relVendorPath) === 0) { // path starts with vendor dir $path = substr($path, strlen($relVendorPath)); $baseDir = '$vendorDir . '; } else { $path = '/' . $path; $baseDir = '$baseDir . '; } } elseif (strpos($path, $vendorPath) === 0) { $path = substr($path, strlen($vendorPath)); $baseDir = '$vendorDir . '; } return $baseDir . var_export($path, true); }
protected function generateDefaultAlias(PackageInterface $package) { $fs = new Filesystem(); $vendorDir = $fs->normalizePath($this->vendorDir); $autoload = $package->getAutoload(); $aliases = []; if (!empty($autoload['psr-0'])) { foreach ($autoload['psr-0'] as $name => $path) { $name = str_replace('\\', '/', trim($name, '\\')); if (!$fs->isAbsolutePath($path)) { $path = $this->vendorDir . '/' . $package->getPrettyName() . '/' . $path; } $path = $fs->normalizePath($path); if (strpos($path . '/', $vendorDir . '/') === 0) { $aliases["@{$name}"] = '<vendor-dir>' . substr($path, strlen($vendorDir)) . '/' . $name; } else { $aliases["@{$name}"] = $path . '/' . $name; } } } if (!empty($autoload['psr-4'])) { foreach ($autoload['psr-4'] as $name => $path) { if (is_array($path)) { // ignore psr-4 autoload specifications with multiple search paths // we can not convert them into aliases as they are ambiguous continue; } $name = str_replace('\\', '/', trim($name, '\\')); if (!$fs->isAbsolutePath($path)) { $path = $this->vendorDir . '/' . $package->getPrettyName() . '/' . $path; } $path = $fs->normalizePath($path); if (strpos($path . '/', $vendorDir . '/') === 0) { $aliases["@{$name}"] = '<vendor-dir>' . substr($path, strlen($vendorDir)); } else { $aliases["@{$name}"] = $path; } } } return $aliases; }
public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) { $filesystem = new Filesystem(); $filesystem->ensureDirectoryExists($installationManager->getVendorPath()); $filesystem->ensureDirectoryExists($targetDir); $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/'); $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true); $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir); $namespacesFile = <<<EOF <?php // autoload_namespace.php generated by Composer \$vendorDir = {$vendorDirCode}; \$baseDir = {$appBaseDir}; return array( EOF; $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages()); $autoloads = $this->parseAutoloads($packageMap); foreach ($autoloads['psr-0'] as $namespace => $paths) { $exportedPaths = array(); foreach ($paths as $path) { $path = strtr($path, '\\', '/'); $baseDir = ''; if (!$filesystem->isAbsolutePath($path)) { if (strpos($path, $relVendorPath) === 0) { // path starts with vendor dir $path = substr($path, strlen($relVendorPath)); $baseDir = '$vendorDir . '; } else { $path = '/' . $path; $baseDir = '$baseDir . '; } } elseif (strpos($path, $vendorPath) === 0) { $path = substr($path, strlen($vendorPath)); $baseDir = '$vendorDir . '; } $exportedPaths[] = $baseDir . var_export($path, true); } $exportedPrefix = var_export($namespace, true); $namespacesFile .= " {$exportedPrefix} => "; if (count($exportedPaths) > 1) { $namespacesFile .= "array(" . implode(', ', $exportedPaths) . "),\n"; } else { $namespacesFile .= $exportedPaths[0] . ",\n"; } } $namespacesFile .= ");\n"; $classmapFile = <<<EOF <?php // autoload_classmap.php generated by Composer \$vendorDir = {$vendorDirCode}; \$baseDir = {$appBaseDir}; return array( EOF; // flatten array $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap'])); foreach ($autoloads['classmap'] as $dir) { foreach (ClassMapGenerator::createMap($dir) as $class => $path) { $path = '/' . $filesystem->findShortestPath(getcwd(), $path, true); $classmapFile .= ' ' . var_export($class, true) . ' => $baseDir . ' . var_export($path, true) . ",\n"; } } $classmapFile .= ");\n"; file_put_contents($targetDir . '/autoload_namespaces.php', $namespacesFile); file_put_contents($targetDir . '/autoload_classmap.php', $classmapFile); if ($includePathFile = $this->getIncludePathsFile($packageMap)) { file_put_contents($targetDir . '/include_paths.php', $includePathFile); } file_put_contents($targetDir . '/autoload.php', $this->getAutoloadFile(true, true, (bool) $includePathFile)); copy(__DIR__ . '/ClassLoader.php', $targetDir . '/ClassLoader.php'); }