Overview

Namespaces

  • CzProject
    • Runner

Classes

  • CzProject\Runner\Runner
  • CzProject\Runner\RunnerResult

Exceptions

  • CzProject\Runner\Exception
  • CzProject\Runner\RunnerException
  • 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: 
<?php

    namespace CzProject\Runner;

    use CzProject\PathHelper;


    class Runner
    {
        /** @var string */
        private $directory;


        public function __construct($directory)
        {
            $this->directory = PathHelper::absolutizePath($directory);
        }


        /**
         * @param  string|string[]
         * @param  string|NULL
         * @return RunnerResult
         */
        public function run($command, $subdirectory = NULL)
        {
            $directory = $this->getDirectory($subdirectory);

            if (!is_dir($directory)) {
                throw new RunnerException("Directory '$directory' not found");
            }

            $cwd = getcwd();
            chdir($directory);

            $cmd = NULL;

            if (is_string($command)) {
                $cmd = $command;

            } else {
                $cmd = $this->processCommand((array) $command);
            }

            exec($cmd . ' 2>&1', $output, $returnCode);
            chdir($cwd);
            return new RunnerResult($cmd, $returnCode, $output);
        }


        /**
         * @param  string|NULL
         * @return string
         */
        public function getDirectory($subdirectory = NULL)
        {
            $directory = $this->directory;

            if ($subdirectory !== NULL) {
                $directory = rtrim($directory . '/' . PathHelper::absolutizePath($subdirectory, NULL), '/');
            }

            return $directory;
        }


        /**
         * @param  string
         * @return string
         */
        public function escapeArgument($argument)
        {
            return escapeshellarg($argument);
        }


        /**
         * Example: [
         *   'program',
         *   '--directory' => 'test',
         *   '--bare',
         *   'value'
         * ]
         * @param  array
         * @return string
         */
        protected function processCommand(array $args)
        {
            $cmd = array();

            $programName = $this->escapeArgument(array_shift($args));

            foreach ($args as $opt => $arg) {
                if (is_string($opt)) {
                    if ($arg !== FALSE && $arg !== NULL) {
                        $cmd[] = $opt;
                    }
                }

                if (!is_bool($arg)) {
                    $cmd[] = $this->escapeArgument($arg);
                }
            }

            return "$programName " . implode(' ', $cmd);
        }
    }
czproject/runner v1.0.0 API documentation API documentation generated by ApiGen