Overview

Namespaces

  • NetBazzlineZfCliGenerator
    • Controller
      • Console
    • Service
      • ProcessPipe
        • Filter
        • Transformer

Classes

  • NetBazzlineZfCliGenerator\Controller\Console\IndexController
  • NetBazzlineZfCliGenerator\Controller\Console\IndexControllerFactory
  • NetBazzlineZfCliGenerator\Service\GenerateCliContentFactory
  • NetBazzlineZfCliGenerator\Service\GenerateConfigurationContentFactory
  • NetBazzlineZfCliGenerator\Service\ProcessPipe\Filter\RemoveColorsAndModuleHeadlines
  • NetBazzlineZfCliGenerator\Service\ProcessPipe\Filter\RemoveFirstLinesAndLastLine
  • NetBazzlineZfCliGenerator\Service\ProcessPipe\Filter\RemoveIndexDotPhpFromLines
  • NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer\DumpCliContent
  • NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer\DumpConfigurationContent
  • NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer\FetchApplicationOutput
  • NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer\ParseToConfiguration
  • Overview
  • Namespace
  • Class
  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: 
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2015-07-11
 * @see https://github.com/stevleibelt/examples/blob/master/php/filesystem/makePathsRelativeToEachOther.php
 */

namespace NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer;

use Net\Bazzline\Component\ProcessPipe\ExecutableException;
use Net\Bazzline\Component\ProcessPipe\ExecutableInterface;
use Symfony\Component\Filesystem\Filesystem;

class DumpCliContent implements ExecutableInterface
{
    const INPUT_KEY_PATH_TO_APPLICATION     = 'path_to_application';
    const INPUT_KEY_PATH_TO_AUTOLOAD        = 'path_to_autoload';
    const INPUT_KEY_PATH_TO_CLI             = 'path_to_cli';
    const INPUT_KEY_PATH_TO_CONFIGURATION   = 'path_to_configuration';
    const INPUT_KEY_PREFIX_CLI              = 'prefix_cli';

    /** @var Filesystem */
    private $filesystem;

    /** @var int */
    private $timestamp;

    /**
     * @param Filesystem $filesystem
     */
    public function setFilesystem(Filesystem $filesystem)
    {
        $this->filesystem = $filesystem;
    }

    /**
     * @param int $timestamp
     */
    public function setTimestamp($timestamp)
    {
        $this->timestamp = $timestamp;
    }

    /**
     * @param mixed $input
     * @return mixed
     * @throws ExecutableException
     */
    public function execute($input = null)
    {
        $this->validateInput($input);

        $date                   = date('Y-m-d H:i:s');
        $filesystem             = $this->filesystem;
        $pathToApplication      = $input[self::INPUT_KEY_PATH_TO_APPLICATION];
        $pathToAutoload         = $input[self::INPUT_KEY_PATH_TO_AUTOLOAD];
        $pathToConfiguration    = $input[self::INPUT_KEY_PATH_TO_CONFIGURATION];
        $pathToCli              = dirname($input[self::INPUT_KEY_PATH_TO_CLI]);
        $prefix                 = $input[self::INPUT_KEY_PREFIX_CLI];

        $pathFromCliToAutoload      = DIRECTORY_SEPARATOR . $filesystem->makePathRelative($pathToAutoload, $pathToCli);
        $pathFromCliToApplication   = DIRECTORY_SEPARATOR . $filesystem->makePathRelative($pathToApplication, $pathToCli);
        $pathFromCliToConfiguration = DIRECTORY_SEPARATOR . $filesystem->makePathRelative($pathToConfiguration, $pathToCli);

        //@todo a bug in symfony filesystem?
        $pathFromCliToAutoload      = substr($pathFromCliToAutoload, 0, -1);
        $pathFromCliToApplication   = substr($pathFromCliToApplication, 0, -1);
        $pathFromCliToConfiguration = substr($pathFromCliToConfiguration, 0, -1);

        $output = <<<EOC
#!/usr/bin/env php
<?php
/**
 * @author Net\Bazzline Zf Cli Generator
 * @since $date
 */

use Net\Bazzline\Component\Cli\Readline\ManagerFactory;

require_once __DIR__ . '$pathFromCliToAutoload';

try {
    \$factory           = new ManagerFactory();
    \$manager           = \$factory->create();
    \$callApplication   = function() {
        \$line       = readline_info('line_buffer');
        \$command    = '/usr/bin/env php ' .
            __DIR__ . '$pathFromCliToApplication ' .
            \$line;

        passthru(\$command);
    };
    \$configuration     = require_once(__DIR__ . '$pathFromCliToConfiguration');

    \$manager->setConfiguration(\$configuration);
    \$manager->setPrompt('$prefix');
    \$manager->run();
} catch (Exception \$exception) {
    echo '----------------' . PHP_EOL;
    echo \$exception->getMessage() . PHP_EOL;
    return 1;
}
EOC;

        return $output;
    }

    /**
     * @param mixed $input
     * @throws ExecutableException
     */
    private function validateInput(&$input)
    {
        if (!is_array($input)) {
            throw new ExecutableException(
                'input must be an array'
            );
        }

        if (empty($input)) {
            throw new ExecutableException(
                'empty input provided'
            );
        }

        $keys = array(
            self::INPUT_KEY_PATH_TO_APPLICATION,
            self::INPUT_KEY_PATH_TO_AUTOLOAD,
            self::INPUT_KEY_PATH_TO_CONFIGURATION,
            self::INPUT_KEY_PATH_TO_CLI,
            self::INPUT_KEY_PREFIX_CLI
        );

        foreach ($keys as $key) {
            if (!isset($input[$key])) {
                throw new ExecutableException(
                    'input must contain mandatory key "' . $key . '"'
                );
            }
        }

        //validate paths
        $paths = array(
            self::INPUT_KEY_PATH_TO_APPLICATION,
            self::INPUT_KEY_PATH_TO_AUTOLOAD,
            self::INPUT_KEY_PATH_TO_CLI,
            self::INPUT_KEY_PATH_TO_CONFIGURATION
        );

        foreach ($paths as $path) {
            $realPath = realpath($input[$path]);

            if ($realPath === false) {
                throw new ExecutableException(
                    'provided path is not a real path / does not exist "' . $input[$path] . '"'
                );
            } else {
                $input[$path] = $realPath;
            }
        }
    }
}
PHP Command Line Generator Module for Zend Framework 2 by bazzline.net API documentation generated by ApiGen