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
2 changes: 2 additions & 0 deletions src/Console/Commands/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$package->findAutoloaders();
$mover->movePackage($package);
}

$mover->replaceClassmapNames();
}
}
32 changes: 28 additions & 4 deletions src/Mover.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Mover
/** @var \stdClass */
protected $config;

/** @var array */
protected $replacedClasses = [];

public function __construct( $workingDir, $config )
{
$this->workingDir = $workingDir;
Expand All @@ -40,8 +43,6 @@ public function movePackage(Package $package)
$finder = new Finder();
$filesystem = new Filesystem(new Local($this->workingDir));

$replacedClasses = [];

foreach( $package->autoloaders as $autoloader ) {
foreach ($autoloader->paths as $path) {
$source_path = $this->workingDir . '/vendor/' . $package->config->name . '/' . $path;
Expand Down Expand Up @@ -77,16 +78,39 @@ public function movePackage(Package $package)
$contents = $replacer->replace($contents);

if ( is_a($replacer, ClassmapReplacer::class)) {
$replacedClasses = array_merge($replacedClasses, $replacer->replacedClasses);
$this->replacedClasses = array_merge($this->replacedClasses, $replacer->replacedClasses);
}

$filesystem->put($targetFile, $contents);
}
}
}
}
}

public function replaceClassmapNames()
{
$classmap_path = $this->workingDir . $this->config->classmap_directory;
$finder = new Finder();
$finder->files()->in($classmap_path);

// Replace all replaced class names throughout the classmap dependencies here
$filesystem = new Filesystem(new Local($this->workingDir));

foreach ($finder as $file) {
$file_path = str_replace($this->workingDir, '', $file->getRealPath());
$contents = $filesystem->read($file_path);

foreach( $this->replacedClasses as $original => $replacement ) {
$contents = preg_replace_callback(
'/\W('.$original.')(?:\(|\:\:)/U',
Copy link
Contributor

Choose a reason for hiding this comment

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

This will miss a couple of scenarios.

The ones I can think of right now:

  1. type-hints: public function __construct( MyClass $obj );
  2. new without arguments: $obj = new MyClass;
  3. class in string: $class = 'MyClass'; $obj = new $class();

It will also probably break when using relative namespaces, like the following:

namespace Vendor;
throw new Exception\MyException(); // FQCN: \Vendor\Exception\MyException

I'm not really sure how you can add cases like these without getting false positives as well, though. I don't think this can properly be handled by regexes, you should look into using nikic/PHP-Parser perhaps.

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 @schlessera, I would love to make this as bullet proof as I can, but don't want this to distract too much from actually implementing the classmap handler. Improving the replacing logic can obviously be an ongoing process. Can you please post this as an issue, so I can handle it accordingly ( - it would be amazing if the GitHub UI allowed me to promote this comment to an issue, directly from here)?

function ($matches) use ($replacement) {
return str_replace($matches[1], $replacement, $matches[0]);
},
$contents
);
}

$filesystem->put($file_path, $contents);
}
}
}