Handmade solution who looks like an Eclipse plugin
First, download and install PHP Parser from: php-parser-github
a simple example will show you that you can get the class name from a given source code
Example of source code
require 'vendor/autoload.php';
use PhpParser\ParserFactory;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$stmts = $parser->parse('<?php class MyClass_SubDir {private $member;}');
var_dump($stmts);
}
catch (Error $e) {
echo 'Parse Error: ', $e->getMessage();
}
Running it from command line//Output
array(1) {
[0]=>
object(PhpParser\Node\Stmt\Class_)#8 (6) {
["type"]=>
int(0)
["extends"]=>
NULL
["implements"]=>
array(0) {
}
["name"]=>
string(14) "MyClass_SubDir"
["stmts"]=>
array(1) {
[0]=>
object(PhpParser\Node\Stmt\Property)#7 (3) {
...
}
and as you can see, we can get all class that are defined in the current source
--object(PhpParser\Node\Stmt\Class_
|
|___ name : MyClass_SubDir
The goals here are
- extract directory name from a class name, to do that you can use `regex` or `explode`
- Move the current file to the desired directory, you can use `rename` function
Save your php code somewhere ... eclipse_plugin.php for example ..
The next step is to create a batch file that will be plugged with Eclipse
Create a bath file that it will receive 2 arguments from Eclipse
- **$1** : container location - the absolute path of the selected file in Eclipse
- **$2** : resource location - the absolute path and name of the selected file in Eclipse (we will need only $2 :-) )
In the batch file put
php eclipse_plugin.php $1 $2
Note : php must be in the PATH
environment variables
PHP will find his arguments in $argv[0]
and $argv[1]
Configuring Eclipse
- go to Run External Tools > External Tools Configuration > click on Program Section > Press on New Launch Configuration
- in Tab Main, specify the location of your custom batch file
- Also specify his working directory
- in section Arguments, put these arguments ${container_loc} ${resource_loc}
Done !, now when you rename your class in Eclipse, just click on Run Button :)
HTH