Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classmap implementation #4

Merged
merged 15 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Composer/Autoload/NamespaceAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ public function processConfig($config)
array_push( $this->paths, $value);
}
}

public function getSearchNamespace()
{
return $this->namespace;
}

public function getNamespacePath()
{
return '';
}
}
9 changes: 9 additions & 0 deletions src/Composer/Autoload/Psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@

class Psr4 extends NamespaceAutoloader
{
public function getSearchNamespace()
{
return trim($this->namespace, '\\');
}

public function getNamespacePath()
{
return str_replace('\\', '/', $this->namespace);
}
}
40 changes: 18 additions & 22 deletions src/Mover.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace CoenJacobs\Mozart;

use CoenJacobs\Mozart\Composer\Autoload\Psr0;
use CoenJacobs\Mozart\Composer\Autoload\Psr4;
use CoenJacobs\Mozart\Composer\Autoload\NamespaceAutoloader;
use CoenJacobs\Mozart\Composer\Package;
use CoenJacobs\Mozart\Replace\ClassmapReplacer;
use CoenJacobs\Mozart\Replace\NamespaceReplacer;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -39,23 +40,17 @@ public function movePackage(Package $package)
$filesystem = new Filesystem(new Local($this->workingDir));

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

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

$searchNamespace = $autoloader->namespace;

if ( is_a($autoloader, Psr4::class)) {
$searchNamespace = trim($autoloader->namespace, '\\');
}

foreach ($finder as $file) {
if ( is_a($autoloader, Psr0::class)) {
$targetFile = str_replace($this->workingDir, $this->config->dep_directory, $file->getRealPath());
} else {
$namespacePath = str_replace('\\', '/', $autoloader->namespace);
if (is_a($autoloader, NamespaceAutoloader::class)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should prefer instanceof operator instead of is_a() function. The operator doesn't incur a function overhead, and is_a() was only de-deprecated because of popular request. It should have been gone already.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed: 05fea7d

$namespacePath = $autoloader->getNamespacePath();
$targetFile = str_replace($this->workingDir, $this->config->dep_directory . $namespacePath, $file->getRealPath());
} else {
$targetFile = str_replace($this->workingDir, $this->config->classmap_directory, $file->getRealPath());
}

$targetFile = str_replace('/vendor/' . $package->config->name . '/' . $path, '', $targetFile);
Expand All @@ -65,18 +60,19 @@ public function movePackage(Package $package)
$targetFile
);

if ( '.php' == substr($targetFile, '-4', 4 ) ) {
if ('.php' == substr($targetFile, '-4', 4)) {
$contents = $filesystem->read($targetFile);

$contents = preg_replace_callback(
'/'.addslashes($searchNamespace).'([\\\|;])/U',
function($matches) {
$replace = trim($matches[0], $matches[1]);
return $this->config->dep_namespace . $replace . $matches[1];
},
$contents
);
if (is_a($autoloader, NamespaceAutoloader::class)) {
$replacer = new NamespaceReplacer();
$replacer->dep_namespace = $this->config->dep_namespace;
} else {
$replacer = new ClassmapReplacer();
}

$replacer->setAutoloader($autoloader);

$contents = $replacer->replace($contents);
$filesystem->put($targetFile, $contents);
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/Replace/BaseReplacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace CoenJacobs\Mozart\Replace;

use CoenJacobs\Mozart\Composer\Autoload\Autoloader;

abstract class BaseReplacer implements Replacer
{
/** @var Autoloader */
public $autoloader;

public function setAutoloader( $autoloader )
{
$this->autoloader = $autoloader;
}
}
11 changes: 11 additions & 0 deletions src/Replace/ClassmapReplacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace CoenJacobs\Mozart\Replace;

class ClassmapReplacer extends BaseReplacer
{
public function replace( $content )
{
return $content;
}
}
23 changes: 23 additions & 0 deletions src/Replace/NamespaceReplacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace CoenJacobs\Mozart\Replace;

class NamespaceReplacer extends BaseReplacer
{
/** @var string */
public $dep_namespace = '';

public function replace($contents)
{
$searchNamespace = $this->autoloader->getSearchNamespace();

return preg_replace_callback(
'/' . addslashes($searchNamespace) . '([\\\|;])/U',
function ($matches) {
$replace = trim($matches[0], $matches[1]);
return $this->dep_namespace . $replace . $matches[1];
},
$contents
);
}
}
9 changes: 9 additions & 0 deletions src/Replace/Replacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace CoenJacobs\Mozart\Replace;

interface Replacer
{
public function setAutoloader($autoloader);
public function replace($contents);
}