Exemple #1
0
 public function main()
 {
     $dirAnn = new Dir($this->dirs);
     $Arg = new \ReflectionClass('Symfony\\Component\\Console\\Input\\InputArgument');
     $Option = new \ReflectionClass('Symfony\\Component\\Console\\Input\\InputOption');
     $console = new Application();
     foreach (array_merge($dirAnn->getMethods('Cli'), $dirAnn->getFunctions('Cli')) as $function) {
         $zargs = [];
         foreach (array('Arg' => 'InputArgument', 'Option' => 'InputOption') as $ann => $class) {
             $class = 'Symfony\\Component\\Console\\Input\\' . $class;
             foreach ($function->get($ann) as $args) {
                 $args = $args->GetArgs();
                 $name = current($args);
                 $flag = NULL;
                 if (!empty($args[1])) {
                     $flag = 0;
                     foreach (explode("|", $args[1]) as $type) {
                         $flag |= ${$ann}->getConstant($type);
                     }
                 }
                 $hint = empty($args[2]) ? $name : $args[2];
                 if ($ann == 'Arg') {
                     $zargs[] = new $class($name, $flag, $hint);
                 } else {
                     if (!empty($args['default'])) {
                         $zargs[] = new $class($name, null, InputOption::VALUE_OPTIONAL, $hint, $args['default']);
                     } else {
                         $zargs[] = new $class($name, null, $flag, $hint);
                     }
                 }
             }
         }
         foreach ($function->get('Cli') as $annotation) {
             $args = $annotation->GetArgs();
             $name = current($args ?: []);
             $desc = !empty($args[1]) ? $args[1] : '';
             $console->register($name)->setDescription($desc)->setDefinition($zargs)->setCode(function ($input, $output) use($function) {
                 require $function->getFile();
                 if ($function instanceof \Notoj\Object\zFunction) {
                     $callback = $function->getName();
                 } else {
                     $class = $annotation->getClass()->getName();
                     $callback = [new $class(), $function->GetName()];
                 }
                 call_user_func($callback, $input, $output);
             });
         }
     }
     $console->run();
 }
Exemple #2
0
 public function prepare()
 {
     $loader = Remember::wrap('cli', function ($args, $files) {
         $dirAnn = new Dir($args);
         return array($dirAnn->getCallable('cli'), $dirAnn->get('CliPlugin', 'Callable'));
     });
     $Arg = new ReflectionClass('Symfony\\Component\\Console\\Input\\InputArgument');
     $Option = new ReflectionClass('Symfony\\Component\\Console\\Input\\InputOption');
     $console = new Application();
     list($functions, $plugins) = $loader($this->dirs);
     foreach ($plugins as $plugin) {
         $name = current($plugin->getArgs());
         $this->plugins[$name][] = $plugin->getObject();
     }
     foreach ($functions as $function) {
         $opts = array();
         $this->processCommandArgs($opts, 'Argument', $Arg, $function);
         $this->processCommandArgs($opts, 'Option', $Option, $function);
         $this->processWorker($opts, $function);
         $this->processPrompt($opts, $function);
         $this->registerCommand($console, $function, $opts);
     }
     return $console;
 }
Exemple #3
0
 public function generate()
 {
     $output = $this->getOutput();
     if (empty($output)) {
         throw new \RuntimeException("You need to set an output file");
     }
     $cache = new Watch($output . '.cache');
     $dirs = $this->dirs;
     foreach ($this->files as $file) {
         $dirs[] = dirname($file);
     }
     $cache->watchFiles($this->files);
     $cache->watchDirs($dirs);
     if (!$cache->hasChanged()) {
         return;
     }
     $annotations = new Annotations();
     $isCached = $output && file_exists($output);
     $files = array();
     $dirs = array();
     foreach (array_unique($this->files) as $file) {
         $ann = new FileParser($file);
         $ann->getAnnotations($annotations);
         $files[] = $file;
         $dirs[] = dirname($file);
     }
     foreach (array_unique($this->dirs) as $dir) {
         $ann = new DirParser($dir);
         $ann->getAnnotations($annotations);
         $dirs[] = $dir;
         $files = array_merge($files, $ann->getFiles());
     }
     foreach ($files as $file) {
         $dirs[] = dirname($file);
     }
     $cache->watchFiles($files)->watchDirs($dirs);
     $cache->watch();
     foreach ($files as $file) {
         $dirs[] = dirname($file);
     }
     $compiler = new Compiler($this, $annotations);
     if (!empty($output)) {
         file_put_contents($output, $compiler->getOutput());
     }
     return $compiler->getOutput();
 }
Exemple #4
0
 protected function getAnnotations()
 {
     $parser = new DirParser($this->dir);
     $annotations = $parser->getAnnotations();
     return $annotations->get('Validate', 'Class');
 }
Exemple #5
0
 public function getServices()
 {
     $annotations = new Notoj\Annotations();
     foreach ($this->config->getDirectories() as $dir) {
         $dir = new Notoj\Dir($dir);
         $dir->getAnnotations($annotations);
     }
     $services = array();
     foreach ($annotations->get('Worker', true) as $worker) {
         foreach ($worker->get('Worker') as $args) {
             $name = current($args['args'] ?: []);
             if (empty($name)) {
                 continue;
             }
             $services[$name] = new Service($worker, $args['args'], $this);
         }
     }
     return $services;
 }
 protected function addDirs(array $dirs)
 {
     foreach ($dirs as $dir) {
         $dir = new NDir($dir);
         $dir->getAnnotations($this->annotations);
         $this->files = array_merge($this->files, $dir->getFiles());
     }
 }