Example #1
0
 /**
  * The entry method.
  *
  * @return  int
  */
 public function main()
 {
     $dryRun = false;
     $classes = [];
     while (false !== ($c = $this->getOption($v))) {
         switch ($c) {
             case 'n':
                 foreach ($this->parser->parseSpecialValue($v) as $namespace) {
                     $namespace = trim(str_replace('.', '\\', $namespace), '\\');
                     if (false === ($pos = strpos($namespace, '\\'))) {
                         throw new Console\Exception('Namespace %s is too short.', 0, $namespace);
                     }
                     $tail = substr($namespace, strpos($namespace, '\\') + 1);
                     $root = resolve('hoa://Library/' . str_replace('\\', '/', $tail));
                     $classes = array_merge($classes, static::findClasses($root, $namespace));
                 }
                 break;
             case 'c':
                 foreach ($this->parser->parseSpecialValue($v) as $class) {
                     $classes[] = $class;
                 }
                 break;
             case 'd':
                 $dryRun = $v;
                 break;
             case '__ambiguous':
                 $this->resolveOptionAmbiguity($v);
                 break;
             case 'h':
             case '?':
             default:
                 return $this->usage();
         }
     }
     if (empty($classes)) {
         return $this->usage();
     }
     foreach ($classes as $i => $class) {
         $classes[$i] = str_replace('.', '\\', $class);
     }
     $generator = new \Atoum\PraspelExtension\Praspel\Generator();
     $generator->setTestNamespacer(function ($namespace) {
         $parts = explode('\\', $namespace);
         return implode('\\', array_slice($parts, 0, 2)) . '\\Test\\Praspel\\Unit' . (isset($parts[2]) ? '\\' . implode('\\', array_slice($parts, 2)) : '');
     });
     $phpBinary = Core::getPHPBinary() ?: Console\Processus::localte('php');
     $envVariable = '__HOA_ATOUM_PRASPEL_EXTENSION_' . md5(Core::uuid());
     $reflection = null;
     $buffer = null;
     $reflectionner = new Console\Processus($phpBinary);
     $reflectionner->on('input', function (Core\Event\Bucket $bucket) use($envVariable) {
         $bucket->getSource()->writeAll('<?php' . "\n" . 'require_once \'' . dirname(__DIR__) . DS . '.bootstrap.atoum.php\';' . "\n" . '$class = getenv(\'' . $envVariable . '\');' . "\n" . 'if (class_exists(\'\\mageekguy\\atoum\\scripts\\runner\', false)) {' . "\n" . '    \\atoum\\scripts\\runner::disableAutorun();' . "\n" . '}' . "\n" . '$reflection = new \\Atoum\\PraspelExtension\\Praspel\\Reflection\\RClass($class);' . "\n" . 'echo serialize($reflection), "\\n";');
         return false;
     });
     $reflectionner->on('output', function (Core\Event\Bucket $bucket) use(&$buffer) {
         $data = $bucket->getData();
         $buffer .= $data['line'] . "\n";
         return;
     });
     $reflectionner->on('stop', function () use(&$buffer, &$reflection) {
         $handle = @unserialize($buffer);
         if (false === $handle) {
             echo $buffer, "\n";
             return;
         }
         $reflection = $handle;
         return;
     });
     foreach ($classes as $class) {
         $status = $class . ' (in ';
         echo '  ⌛ ', $status;
         putenv($envVariable . '=' . $class);
         $buffer = null;
         $reflection = null;
         $reflectionner->run();
         $output = $generator->generate($reflection);
         $parts = explode('\\', $class);
         $paths = resolve('hoa://Library/' . $parts[1] . '/' . 'Test/Praspel/Unit/' . implode('/', array_slice($parts, 2)) . '.php', false, true);
         $max = 0;
         $thePath = 0;
         foreach ($paths as $path) {
             $length = Ustring\Search::lcp($reflection->getFilename(), $path);
             if ($length > $max) {
                 $thePath = $path;
             }
         }
         $statusTail = (40 < strlen($thePath) ? '…' . substr($thePath, -39) : $thePath) . ')';
         echo $statusTail;
         $status .= $statusTail;
         if (false === $reflection->isInstantiable()) {
             Console\Cursor::clear('↔');
             echo '  ⚡️ ', $status, '; not instantiable.', "\n";
             continue;
         }
         $dirname = dirname($thePath);
         if (false === is_dir($dirname)) {
             if (false === $dryRun) {
                 mkdir($dirname, 0755, true);
             } else {
                 echo "\n", static::info('Creating directory: ' . $dirname . '.');
             }
         }
         if (false === $dryRun) {
             file_put_contents($thePath, $output);
         } else {
             echo "\n", static::info('Content of the ' . $thePath . ':'), "\n";
             Console\Cursor::colorize('foreground(yellow)');
             echo '    ┏', "\n", '    ┃  ', str_replace("\n", "\n" . '    ┃  ', trim($output)), "\n", '    ┗', "\n";
             Console\Cursor::colorize('foreground(normal)');
         }
         Console\Cursor::clear('↔');
         echo '  ', Console\Chrome\Text::colorize('✔︎', 'foreground(green)'), ' ', $status, "\n";
     }
     return;
 }
Example #2
0
 /**
  * Prepare the pipette.
  *
  * @param   array                         $options    The option definition.
  * @param   \Hoa\Console\Core\Cli\Parser  $parser     The parser.
  * @return  void
  * @throws  \Hoa\Console\Exception
  */
 public function __construct(array $options, Parser $parser)
 {
     $this->_options = $options;
     $this->_parser = $parser;
     if (empty($options)) {
         $this->_pipette[null] = null;
         return;
     }
     $names = [];
     foreach ($options as $i => $option) {
         if (isset($option[self::OPTION_NAME])) {
             $names[$option[self::OPTION_NAME]] = $i;
         }
         if (isset($option[self::OPTION_VAL])) {
             $names[$option[self::OPTION_VAL]] = $i;
         }
     }
     $_names = array_keys($names);
     $switches = $parser->getSwitches();
     foreach ($switches as $name => $value) {
         if (false === in_array($name, $_names)) {
             if (1 === strlen($name)) {
                 $this->_pipette[] = ['__ambiguous', ['solutions' => [], 'value' => $value, 'option' => $name]];
                 continue;
             }
             $haystack = implode(';', $_names);
             $differences = (int) ceil(strlen($name) / 3);
             $searched = Ustring\Search::approximated($haystack, $name, $differences);
             $solutions = [];
             foreach ($searched as $s) {
                 $h = substr($haystack, $s['i'], $s['l']);
                 if (false !== strpos($h, ';') || false !== in_array($h, array_keys($switches)) || false === in_array($h, $_names)) {
                     continue;
                 }
                 $solutions[] = $h;
             }
             if (empty($solutions)) {
                 continue;
             }
             $this->_pipette[] = ['__ambiguous', ['solutions' => $solutions, 'value' => $value, 'option' => $name]];
             continue;
         }
         $option = $options[$names[$name]];
         $argument = $option[self::OPTION_HAS_ARG];
         if (self::NO_ARGUMENT === $argument) {
             if (!is_bool($value)) {
                 $parser->transferSwitchToInput($name, $value);
             }
         } elseif (self::REQUIRED_ARGUMENT === $argument && !is_string($value)) {
             throw new Exception('The argument %s requires a value (it is not a switch).', 0, $name);
         }
         $this->_pipette[] = [$option[self::OPTION_VAL], $value];
     }
     $this->_pipette[null] = null;
     reset($this->_pipette);
     return;
 }
Example #3
0
 public function case_approximated()
 {
     $this->given($x = 'GATAA', $y = 'CAGATAAGAGAA', $k = 1)->when($result = LUT\Search::approximated($y, $x, $k))->then->array($result)->isEqualTo([0 => ['i' => 1, 'j' => 6, 'l' => 5], 1 => ['i' => 2, 'j' => 7, 'l' => 5], 2 => ['i' => 3, 'j' => 8, 'l' => 5], 3 => ['i' => 7, 'j' => 12, 'l' => 5]]);
 }