[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diviner/workflow/ -> DivinerAtomizeWorkflow.php (source)

   1  <?php
   2  
   3  final class DivinerAtomizeWorkflow extends DivinerWorkflow {
   4  
   5    public function didConstruct() {
   6      $this
   7        ->setName('atomize')
   8        ->setSynopsis(pht('Build atoms from source.'))
   9        ->setArguments(
  10          array(
  11            array(
  12              'name' => 'atomizer',
  13              'param' => 'class',
  14              'help' => pht('Specify a subclass of DivinerAtomizer.'),
  15            ),
  16            array(
  17              'name' => 'book',
  18              'param' => 'path',
  19              'help' => pht('Path to a Diviner book configuration.'),
  20            ),
  21            array(
  22              'name' => 'files',
  23              'wildcard' => true,
  24            ),
  25            array(
  26              'name' => 'ugly',
  27              'help' => pht('Produce ugly (but faster) output.'),
  28            ),
  29          ));
  30    }
  31  
  32    public function execute(PhutilArgumentParser $args) {
  33      $this->readBookConfiguration($args->getArg('book'));
  34  
  35      $console = PhutilConsole::getConsole();
  36  
  37      $atomizer_class = $args->getArg('atomizer');
  38      if (!$atomizer_class) {
  39        throw new Exception('Specify an atomizer class with --atomizer.');
  40      }
  41  
  42      $symbols = id(new PhutilSymbolLoader())
  43        ->setName($atomizer_class)
  44        ->setConcreteOnly(true)
  45        ->setAncestorClass('DivinerAtomizer')
  46        ->selectAndLoadSymbols();
  47      if (!$symbols) {
  48        throw new Exception(
  49          "Atomizer class '{$atomizer_class}' must be a concrete subclass of ".
  50          "DivinerAtomizer.");
  51      }
  52  
  53      $atomizer = newv($atomizer_class, array());
  54  
  55      $files = $args->getArg('files');
  56      if (!$files) {
  57        throw new Exception('Specify one or more files to atomize.');
  58      }
  59  
  60      $file_atomizer = new DivinerFileAtomizer();
  61  
  62      foreach (array($atomizer, $file_atomizer) as $configure) {
  63        $configure->setBook($this->getConfig('name'));
  64      }
  65  
  66      $group_rules = array();
  67      foreach ($this->getConfig('groups', array()) as $group => $spec) {
  68        $include = (array)idx($spec, 'include', array());
  69        foreach ($include as $pattern) {
  70          $group_rules[$pattern] = $group;
  71        }
  72      }
  73  
  74      $all_atoms = array();
  75      $context = array(
  76        'group' => null,
  77      );
  78      foreach ($files as $file) {
  79        $abs_path = Filesystem::resolvePath($file, $this->getConfig('root'));
  80        $data = Filesystem::readFile($abs_path);
  81  
  82        if (!$this->shouldAtomizeFile($file, $data)) {
  83          $console->writeLog("Skipping %s...\n", $file);
  84          continue;
  85        } else {
  86          $console->writeLog("Atomizing %s...\n", $file);
  87        }
  88  
  89        $context['group'] = null;
  90        foreach ($group_rules as $rule => $group) {
  91          if (preg_match($rule, $file)) {
  92            $context['group'] = $group;
  93            break;
  94          }
  95        }
  96  
  97        $file_atoms = $file_atomizer->atomize($file, $data, $context);
  98        $all_atoms[] = $file_atoms;
  99  
 100        if (count($file_atoms) !== 1) {
 101          throw new Exception('Expected exactly one atom from file atomizer.');
 102        }
 103        $file_atom = head($file_atoms);
 104  
 105        $atoms = $atomizer->atomize($file, $data, $context);
 106  
 107        foreach ($atoms as $atom) {
 108          if (!$atom->hasParent()) {
 109            $file_atom->addChild($atom);
 110          }
 111        }
 112  
 113        $all_atoms[] = $atoms;
 114      }
 115  
 116      $all_atoms = array_mergev($all_atoms);
 117  
 118      $all_atoms = mpull($all_atoms, 'toDictionary');
 119      $all_atoms = ipull($all_atoms, null, 'hash');
 120  
 121      if ($args->getArg('ugly')) {
 122        $json = json_encode($all_atoms);
 123      } else {
 124        $json_encoder = new PhutilJSON();
 125        $json = $json_encoder->encodeFormatted($all_atoms);
 126      }
 127  
 128      $console->writeOut('%s', $json);
 129  
 130      return 0;
 131    }
 132  
 133    private function shouldAtomizeFile($file_name, $file_data) {
 134      return (strpos($file_data, '@'.'undivinable') === false);
 135    }
 136  
 137  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1