Exemple #1
0
 public function init()
 {
     $this->args = new SimpleArgs();
     if ($this->args->flag('help')) {
         $this->showHelp();
         exit;
     }
     $this->isVerbose = $this->args->flag('v') !== false;
     $this->verbose("Verbose: ON");
     // check parameters
     $webRoot = $this->args->flag('webroot');
     $infile = $this->args->flag('infile');
     if (!is_string($webRoot) || !is_string($infile)) {
         $this->throwError("Missing argument(s)");
     }
     if (!file_exists($infile)) {
         $this->throwError("File not found: %s", $infile);
     }
     // set webroot qualified path
     $webRoot = realpath($webRoot);
     // constants file
     $constants = array();
     $configFile = $this->args->flag('config');
     if (!file_exists($configFile)) {
         $this->throwError("File not found: %s", $configFile);
     } else {
         $constants = (require $configFile);
         $this->verbose('Constants: %s', implode(', ', array_keys($constants)));
     }
     $options = array('VERBOSE' => $this->isVerbose, 'STRIP' => $this->args->flag('strip'), 'WEB_PATH' => $webRoot, 'WEB_EXCL' => '*.psd,*.txt,*.bak,*.css,*.js', 'CLI' => $this);
     // auto output file naming
     if (false !== strstr($infile, Juicer::FILE_PATTERN_JUICY)) {
         $outfile = str_replace(Juicer::FILE_PATTERN_JUICY, Juicer::FILE_PATTERN_JUICED, $infile);
     } else {
         $this->throwError('The default output file naming requires "*.juicy.*" pattern.');
     }
     $juicer = new Juicer($options, $constants);
     // start profiling script speed
     $juicer->profileStart();
     try {
         $contents = $juicer->juice($infile);
     } catch (Exception $e) {
         $this->throwError('EXCEPTION: ' . $e->getMessage());
     }
     // end profiling script speed
     $this->verbose('Execution time: %s seconds.', $juicer->profileEnd());
     $this->verbose('Output file: "%s".', $outfile);
     if (file_put_contents($outfile, $contents) === false) {
         $this->throwError("Error writing to outfile %s", $outfile);
     }
     $this->verbose('Success!');
 }