Skip to content

Commit

Permalink
Initial code to modify composer's autoloader after baking a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 17, 2014
1 parent 6eb0fe7 commit 8d90a45
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
53 changes: 49 additions & 4 deletions src/Console/Command/Task/PluginTask.php
Expand Up @@ -39,7 +39,7 @@ class PluginTask extends BakeTask {
*
* @var array
*/
public $tasks = ['Template'];
public $tasks = ['Template', 'Project'];

/**
* initialize
Expand Down Expand Up @@ -141,7 +141,8 @@ public function bake($plugin) {
$out .= "}\n";
$this->createFile($this->path . $plugin . DS . $classBase . DS . 'Controller' . DS . $controllerFileName, $out);

$this->_modifyBootstrap($plugin);
$hasAutoloader = $this->_modifyAutoloader($plugin, $this->path);
$this->_modifyBootstrap($plugin, $hasAutoloader);
$this->_generatePhpunitXml($plugin, $this->path);
$this->_generateTestBootstrap($plugin, $this->path);

Expand All @@ -156,13 +157,20 @@ public function bake($plugin) {
* Update the app's bootstrap.php file.
*
* @param string $plugin Name of plugin
* @param boolean $hasAutoloader Whether or there is an autoloader configured for
* the plugin
* @return void
*/
protected function _modifyBootstrap($plugin) {
protected function _modifyBootstrap($plugin, $hasAutoloader) {
$bootstrap = new File($this->bootstrap, false);
$contents = $bootstrap->read();
if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
$bootstrap->append("\nPlugin::load('$plugin', ['bootstrap' => false, 'routes' => false]);\n");
$autoload = $hasAutoloader ? null : "'autoload' => true, ";
$bootstrap->append(sprintf(
"\nPlugin::load('%s', [%s'bootstrap' => false, 'routes' => false]);\n",
$plugin,
$autoload
));
$this->out('');
$this->out(sprintf('%s modified', $this->bootstrap));
}
Expand Down Expand Up @@ -205,6 +213,43 @@ protected function _generateTestBootstrap($plugin, $path) {
$this->createFile($file, $out);
}

protected function _modifyAutoloader($plugin, $path) {
$path = dirname($path);

if (!file_exists($path . DS . 'composer.json')) {
return false;
}

$file = $path . DS . 'composer.json';
$config = json_decode(file_get_contents($file), true);
$config['autoload']['psr-4'][$plugin . '\\'] = "./Plugin/$plugin/src";

$this->out(__d('cake_console', '<info>Modifying composer autoloader</info>'));

file_put_contents(
$file,
json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
);

$composer = $this->Project->findComposer();

if (!$composer) {
$this->error(__d('cake_console', 'Could not locate composer, Add composer to your PATH, or use the -composer option.'));
return false;
}

try {
$command = 'php ' . escapeshellarg($composer) . ' dump-autoload ';
$this->Project->callComposer($command);
} catch (\RuntimeException $e) {
$error = $e->getMessage();
$this->error(__d('cake_console', 'Could not run `composer dump-autoload`: %s', $error));
return false;
}

return true;
}

/**
* find and change $this->path to the user selection
*
Expand Down
17 changes: 14 additions & 3 deletions src/Console/Command/Task/ProjectTask.php
Expand Up @@ -146,6 +146,18 @@ public function bake($path) {

$command = 'php ' . escapeshellarg($composer) . ' create-project -s dev cakephp/app ' . escapeshellarg($path);

try {
$this->callComposer($command);
} catch (\RuntimeException $e) {
$error = $e->getMessage();
$this->error(__d('cake_console', 'Installation from packagist.org failed with: %s', $error));
return false;
}

return true;
}

public function callComposer($command) {
$descriptorSpec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
Expand All @@ -172,11 +184,10 @@ public function bake($path) {
proc_close($process);

if ($error) {
$this->error(__d('cake_console', 'Installation from packagist.org failed with: %s', $error));
return false;
throw new \RuntimeException($error);
}

$this->out($output);
return true;
}

/**
Expand Down

0 comments on commit 8d90a45

Please sign in to comment.