Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
coenjacobs committed Mar 6, 2017
0 parents commit 7ad402f275689ca373b451caf998b18c9b795e9f
@@ -0,0 +1,5 @@
.idea/
vendor/

.DS_Store
composer.lock
@@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php
call_user_func(function ($version) {
if (is_file($autoload = getcwd() . '/vendor/autoload.php')) {
require $autoload;
} elseif (is_file($autoload = getcwd() . '/../../autoload.php')) {
require $autoload;
}
if (is_file($autoload = __DIR__ . '/../vendor/autoload.php')) {
require($autoload);
} elseif (is_file($autoload = __DIR__ . '/../../../autoload.php')) {
require($autoload);
} else {
fwrite(STDERR,
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
exit(1);
}
$app = new CoenJacobs\Mozart\Console\Application($version);
$app->run();
}, '0.1.0');
@@ -0,0 +1,22 @@
{
"name": "coenjacobs/mozart",
"authors": [
{
"name": "Coen Jacobs",
"email": "coenjacobs@gmail.com"
}
],
"bin": ["bin/mozart"],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"symfony/console": "^3.2",
"symfony/finder": "^3.2",
"league/flysystem": "^1.0"
},
"autoload": {
"psr-4": {
"CoenJacobs\\Mozart\\": "src/"
}
}
}
@@ -0,0 +1,8 @@
<?php
namespace CoenJacobs\Mozart\Composer\Autoload;
interface Autoloader
{
}
@@ -0,0 +1,12 @@
<?php
namespace CoenJacobs\Mozart\Composer\Autoload;
class Psr0 implements Autoloader
{
/** @var string */
public $namespace = '';
/** @var array */
public $paths = [];
}
@@ -0,0 +1,12 @@
<?php
namespace CoenJacobs\Mozart\Composer\Autoload;
class Psr4 implements Autoloader
{
/** @var string */
public $namespace = '';
/** @var array */
public $paths = [];
}
@@ -0,0 +1,46 @@
<?php
namespace CoenJacobs\Mozart\Composer;
class Package
{
/** @var string */
public $path = '';
/** @var */
public $config;
/** @var array */
public $autoloaders = [];
public function __construct( $path )
{
$this->path = $path;
$this->config = json_decode(file_get_contents($this->path . '/composer.json'));
}
public function findAutoloaders()
{
$namespace_autoloaders = array(
'psr-0' => 'CoenJacobs\Mozart\Composer\Autoload\Psr0',
'psr-4' => 'CoenJacobs\Mozart\Composer\Autoload\Psr4',
);
if ( ! isset( $this->config->autoload ) ) return;
foreach( $namespace_autoloaders as $key => $value ) {
if ( ! isset( $this->config->autoload->$key) ) continue;
$autoconfigs = (array)$this->config->autoload->$key;
$autoloader = new $value();
foreach( $autoconfigs as $key2 => $value2) {
$autoloader->namespace = $key2;
array_push( $autoloader->paths, $value2);
}
array_push($this->autoloaders, $autoloader);
}
}
}
@@ -0,0 +1,20 @@
<?php
namespace CoenJacobs\Mozart\Console;
use CoenJacobs\Mozart\Console\Commands\Compose;
use Symfony\Component\Console\Application as BaseApplication;
class Application extends BaseApplication
{
/**
* @param string $version
*/
public function __construct($version)
{
parent::__construct('mozart', $version);
$composeCommand = new Compose();
$this->add($composeCommand);
}
}
@@ -0,0 +1,36 @@
<?php
namespace CoenJacobs\Mozart\Console\Commands;
use CoenJacobs\Mozart\Composer\Package;
use CoenJacobs\Mozart\Mover;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Compose extends Command
{
protected function configure()
{
$this->setName('compose');
$this->setDescription('Composes all dependencies as a package inside a WordPress plugin.');
$this->setHelp('');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$workingDir = getcwd();
$config = json_decode(file_get_contents($workingDir . '/composer.json'));
$config = $config->extra->mozart;
$mover = new Mover($workingDir, $config);
$mover->deleteTargetDir();
foreach( $config->packages as $package_slug ) {
$package = new Package($workingDir . '/vendor/' . $package_slug);
$package->findAutoloaders();
$mover->movePackage($package);
}
}
}
@@ -0,0 +1,86 @@
<?php
namespace CoenJacobs\Mozart;
use CoenJacobs\Mozart\Composer\Autoload\Psr0;
use CoenJacobs\Mozart\Composer\Autoload\Psr4;
use CoenJacobs\Mozart\Composer\Package;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\Finder\Finder;
class Mover
{
/** @var string */
protected $workingDir;
/** @var string */
protected $targetDir;
/** @var \stdClass */
protected $config;
public function __construct( $workingDir, $config )
{
$this->workingDir = $workingDir;
$this->targetDir = $config->dep_directory;
$this->config = $config;
}
public function deleteTargetDir()
{
$filesystem = new Filesystem(new Local($this->workingDir));
$filesystem->deleteDir($this->targetDir);
}
public function movePackage(Package $package)
{
$finder = new Finder();
$filesystem = new Filesystem(new Local($this->workingDir));
foreach( $package->autoloaders as $autoloader ) {
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);
$targetFile = str_replace($this->workingDir, $this->config->dep_directory . $namespacePath, $file->getRealPath());
}
$targetFile = str_replace('/vendor/' . $package->config->name . '/' . $path, '', $targetFile);
$filesystem->copy(
str_replace($this->workingDir, '', $file->getRealPath()),
$targetFile
);
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
);
$filesystem->put($targetFile, $contents);
}
}
}
}
}
}

0 comments on commit 7ad402f

Please sign in to comment.
You can’t perform that action at this time.