Skip to content

Commit

Permalink
Merge branch 'Don't-infer-vendor-directory-from-package-name.-Fix-for-#…
Browse files Browse the repository at this point in the history
…97' into config-value-object
  • Loading branch information
BrianHenryIE committed Feb 7, 2021
2 parents 6a80efb + 9cd2f71 commit 2ececbb
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ vendor/
.DS_Store
composer.lock
.phpunit.result.cache

temptestdir
12 changes: 4 additions & 8 deletions src/Composer/Autoload/NamespaceAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CoenJacobs\Mozart\Composer\Autoload;

use stdClass;

abstract class NamespaceAutoloader implements Autoloader
{
/** @var string */
Expand All @@ -17,7 +19,7 @@ abstract class NamespaceAutoloader implements Autoloader
public $paths = [];

/**
* A package's composer.json config autoload key's value, where $key is `psr-1`|`psr-4`|`classmap`.
* A package's composer.json config autoload key's value, where $key is `psr-0`|`psr-4`|`classmap`.
*
* @param $autoloadConfig
*
Expand All @@ -39,11 +41,5 @@ public function getSearchNamespace()
return $this->namespace;
}

/**
* @return string
*/
public function getNamespacePath()
{
return '';
}
abstract public function getNamespacePath(): string;
}
8 changes: 8 additions & 0 deletions src/Composer/Autoload/Psr0.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php
/**
* @see https://www.php-fig.org/psr/psr-0/
*/

namespace CoenJacobs\Mozart\Composer\Autoload;

class Psr0 extends NamespaceAutoloader
{

public function getNamespacePath(): string
{
return '';
}
}
14 changes: 6 additions & 8 deletions src/Composer/Autoload/Psr4.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?php
/**
* @see https://www.php-fig.org/psr/psr-4/
*/

namespace CoenJacobs\Mozart\Composer\Autoload;

class Psr4 extends NamespaceAutoloader
{
/**
* @return string
*/
public function getSearchNamespace()

public function getSearchNamespace(): string
{
return trim($this->namespace, '\\');
}

/**
* @return string
*/
public function getNamespacePath()
public function getNamespacePath(): string
{
return str_replace('\\', DIRECTORY_SEPARATOR, $this->namespace);
}
Expand Down
106 changes: 56 additions & 50 deletions src/Mover.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@

class Mover
{
/** @var string */
/**
* The only path variable with a leading slash.
*
* @var string
*/
protected $workingDir;

/** @var string */
protected $targetDir;
protected $dep_directory;

/** @var string */
protected $classmap_directory;

/** @var MozartConfig */
protected $config;
Expand All @@ -39,6 +46,11 @@ public function __construct($workingDir, MozartConfig $config)
$this->workingDir = $workingDir;
$this->targetDir = $this->config->getDepDirectory();

$this->workingDir = $workingDir;

$this->dep_directory = $this->clean($config->getDepDirectory());
$this->classmap_directory = $this->clean($config->getClassmapDirectory());

$this->filesystem = new Filesystem(new Local($this->workingDir));
}

Expand Down Expand Up @@ -77,13 +89,12 @@ private function deleteDepTargetDirs($package): void
switch ($autoloaderType) {
case Psr0::class:
case Psr4::class:
$outputDir = $this->config->getDepDirectory() . $packageAutoloader->namespace;
$outputDir = str_replace('\\', DIRECTORY_SEPARATOR, $outputDir);
$outputDir = $this->dep_directory . DIRECTORY_SEPARATOR .
$this->clean($packageAutoloader->namespace);
$this->filesystem->deleteDir($outputDir);
break;
case Classmap::class:
$outputDir = $this->config->getClassmapDirectory() . $package->config->name;
$outputDir = str_replace('\\', DIRECTORY_SEPARATOR, $outputDir);
$outputDir = $this->classmap_directory . DIRECTORY_SEPARATOR . $this->clean($package->config->name);
$this->filesystem->deleteDir($outputDir);
break;
}
Expand All @@ -96,12 +107,12 @@ private function deleteDepTargetDirs($package): void

public function deleteEmptyDirs(): void
{
if (count($this->filesystem->listContents($this->config->getDepDirectory(), true)) === 0) {
$this->filesystem->deleteDir($this->config->getDepDirectory());
if (count($this->filesystem->listContents($this->dep_directory, true)) === 0) {
$this->filesystem->deleteDir($this->dep_directory);
}

if (count($this->filesystem->listContents($this->config->getClassmapDirectory(), true)) === 0) {
$this->filesystem->deleteDir($this->config->getClassmapDirectory());
if (count($this->filesystem->listContents($this->classmap_directory, true)) === 0) {
$this->filesystem->deleteDir($this->classmap_directory);
}
}

Expand All @@ -121,10 +132,7 @@ public function movePackage(ComposerPackageConfig $package)
$finder = new Finder();

foreach ($autoloader->paths as $path) {
$source_path = $this->workingDir . 'vendor' . DIRECTORY_SEPARATOR
. $package->config->name . DIRECTORY_SEPARATOR . $path;

$source_path = str_replace('/', DIRECTORY_SEPARATOR, $source_path);
$source_path = DIRECTORY_SEPARATOR . $this->clean($package->path . DIRECTORY_SEPARATOR . $path);

$finder->files()->in($source_path);

Expand All @@ -142,8 +150,7 @@ public function movePackage(ComposerPackageConfig $package)


foreach ($autoloader->files as $file) {
$source_path = $this->workingDir . 'vendor'
. DIRECTORY_SEPARATOR . $package->config->name;
$source_path = DIRECTORY_SEPARATOR . $this->clean($package->path);

$finder->files()->name($file)->in($source_path);

Expand All @@ -156,8 +163,7 @@ public function movePackage(ComposerPackageConfig $package)
$finder = new Finder();

foreach ($autoloader->paths as $path) {
$source_path = $this->workingDir . 'vendor'
. DIRECTORY_SEPARATOR . $package->config->name . DIRECTORY_SEPARATOR . $path;
$source_path = DIRECTORY_SEPARATOR . $this->clean($package->path . DIRECTORY_SEPARATOR . $path);

$finder->files()->in($source_path);

Expand Down Expand Up @@ -193,47 +199,31 @@ public function movePackage(ComposerPackageConfig $package)
*/
public function moveFile(ComposerPackageConfig $package, $autoloader, $file, $path = '')
{
$sourceFileAbsolutePath = $file->getPathname();
if ($autoloader instanceof NamespaceAutoloader) {
$namespacePath = strtolower($autoloader->getNamespacePath());
$replaceWith = $this->config->getDepDirectory() . $namespacePath;
// The relative path to the file from the project root.
$sourceFilePath = $this->clean(str_replace($this->workingDir, '', $file->getPathname()));

$packagePath = $this->clean(str_replace($this->workingDir, '', $package->path));

$findString = $this->workingDir . 'vendor' . DIRECTORY_SEPARATOR . $namespacePath . $path;
if ($autoloader instanceof NamespaceAutoloader) {
$namespacePath = $this->clean($autoloader->getNamespacePath());

$targetFileRelativePath = str_replace($findString, $replaceWith, $sourceFileAbsolutePath);
// TODO: Should $path come from the NameSpaceAutoloader object?
$sourceVendorPath = $this->clean($packagePath . DIRECTORY_SEPARATOR . $path);

$destinationMozartPath = $this->dep_directory . DIRECTORY_SEPARATOR . $namespacePath;

$packageVendorPath = $this->workingDir . 'vendor' . DIRECTORY_SEPARATOR . $package->config->name
. DIRECTORY_SEPARATOR . $path;
$packageVendorPath = str_replace('/', DIRECTORY_SEPARATOR, $packageVendorPath);
$targetFileRelativePath = str_replace($packageVendorPath, '', $targetFileRelativePath);
$targetFilePath = str_ireplace($sourceVendorPath, $destinationMozartPath, $sourceFilePath);
} else {
$namespacePath = $package->config->name;
$replaceWith = $this->config->getClassmapDirectory() . $namespacePath;
$targetFileRelativePath = str_replace($this->workingDir, $replaceWith, $file->getPathname());
$packageName = $this->clean($package->config->name);

$packageVendorPath = 'vendor' . DIRECTORY_SEPARATOR . $package->config->name
. DIRECTORY_SEPARATOR;
$destinationMozartPath = $this->classmap_directory . DIRECTORY_SEPARATOR . $packageName;

$packageVendorPath = str_replace('/', DIRECTORY_SEPARATOR, $packageVendorPath);
$targetFilePath = str_ireplace($packagePath, $destinationMozartPath, $sourceFilePath);
}

$sourceFileRelativePath = str_replace($this->workingDir, '', $sourceFileAbsolutePath);
$targetFileRelativePath = str_replace($packageVendorPath, DIRECTORY_SEPARATOR, $targetFileRelativePath);

try {
$this->filesystem->copy($sourceFileRelativePath, $targetFileRelativePath);
} catch (FileNotFoundException $e) {
throw $e;
} catch (FileExistsException $e) {
if (md5_file($sourceFileAbsolutePath) === md5_file($this->workingDir . $targetFileRelativePath)) {
return $targetFileRelativePath;
} else {
throw $e;
}
}
$this->filesystem->copy($sourceFilePath, $targetFilePath);

return $targetFileRelativePath;
return $targetFilePath;
}

/**
Expand All @@ -246,7 +236,7 @@ public function moveFile(ComposerPackageConfig $package, $autoloader, $file, $pa
protected function deletePackageVendorDirectories(): void
{
foreach ($this->movedPackages as $movedPackage) {
$packageDir = 'vendor' . DIRECTORY_SEPARATOR . $movedPackage;
$packageDir = 'vendor' . DIRECTORY_SEPARATOR . $this->clean($movedPackage);
if (!is_dir($packageDir) || is_link($packageDir)) {
continue;
}
Expand All @@ -267,4 +257,20 @@ private function dirIsEmpty(string $dir): bool
$di = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
return iterator_count($di) === 0;
}

/**
* For Windows & Unix file paths' compatibility.
*
* * Removes duplicate `\` and `/`.
* * Trims them from each end.
* * Replaces them with the OS agnostic DIRECTORY_SEPARATOR.
*
* @param string $path A full or partial filepath.
*
* @return string
*/
protected function clean($path)
{
return trim(preg_replace('/[\/\\\\]+/', DIRECTORY_SEPARATOR, $path), DIRECTORY_SEPARATOR);
}
}
Loading

0 comments on commit 2ececbb

Please sign in to comment.