1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175:
<?php
namespace NetBazzlineZfCliGenerator\Controller\Console;
use Exception;
use Net\Bazzline\Component\ProcessPipe\PipeInterface;
use NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer\DumpCliContent;
use ZfConsoleHelper\Controller\Console\AbstractConsoleController;
class IndexController extends AbstractConsoleController
{
private $generateCli;
private $generateConfiguration;
private $pathToApplication;
private $pathToAutoload;
private $pathToCli;
private $pathToConfiguration;
private $prefix;
public function injectGenerateCliProcessPipe(PipeInterface $processPipe)
{
$this->generateCli = $processPipe;
}
public function injectGenerateConfigurationProcessPipe(PipeInterface $processPipe)
{
$this->generateConfiguration = $processPipe;
}
public function injectPathToApplication($path)
{
$this->pathToApplication = $path;
}
public function injectPathToAutoload($path)
{
$this->pathToAutoload = $path;
}
public function injectPathToCli($path)
{
$this->pathToCli = $path;
}
public function injectPathToConfiguration($path)
{
$this->pathToConfiguration = $path;
}
public function injectPrefix($prefix)
{
$this->prefix = $prefix;
}
public function configurationAction()
{
$console = $this->getConsole();
$pathToApplication = $this->pathToApplication;
$pathToConfiguration = $this->pathToConfiguration;
$processPipe = $this->generateConfiguration;
try {
$this->throwExceptionIfNotCalledInsideAnCliEnvironment();
$content = $processPipe->execute($pathToApplication);
$this->tryToWriteContent($pathToConfiguration, $content);
$console->writeLine('generated configuration in path: "' . $pathToConfiguration . '"');
} catch (Exception $exception) {
$this->handleException($exception);
}
}
public function cliAction()
{
$console = $this->getConsole();
$pathToApplication = $this->pathToApplication;
$pathToAutoload = $this->pathToAutoload;
$pathToConfiguration = $this->pathToConfiguration;
$pathToCli = $this->pathToCli;
$prefix = $this->prefix;
$processPipe = $this->generateCli;
try {
$this->throwExceptionIfNotCalledInsideAnCliEnvironment();
$input = array(
DumpCliContent::INPUT_KEY_PATH_TO_APPLICATION => $pathToApplication,
DumpCliContent::INPUT_KEY_PATH_TO_AUTOLOAD => $pathToAutoload,
DumpCliContent::INPUT_KEY_PATH_TO_CLI => $pathToCli,
DumpCliContent::INPUT_KEY_PATH_TO_CONFIGURATION => $pathToConfiguration,
DumpCliContent::INPUT_KEY_PREFIX_CLI => $prefix
);
if (!file_exists($pathToCli)) {
$this->tryToWriteContent($pathToCli, '');
}
$content = $processPipe->execute($input);
$this->tryToWriteContent($pathToCli, $content);
chmod($pathToCli, 0755);
$console->writeLine('generated cli in path: "' . $pathToCli . '"');
} catch (Exception $exception) {
$this->handleException($exception);
}
}
protected function beVerbose()
{
return $this->hasBooleanParameter('v', 'verbose');
}
private function tryToWriteContent($path, $content)
{
$contentCouldBeWritten = (file_put_contents($path, $content) !== false);
if (!$contentCouldBeWritten) {
throw new Exception(
'could not write to path "' . $path . '"'
);
}
}
}