示例#1
0
文件: Shell.php 项目: huxtable/cli
 /**
  * Execute an external command, generate friendly output and return the result
  * 
  * @param	string	$command
  * @param	boolean	$formatted	Return formatted output in addition to raw
  * @param	string	$prefix
  * @return	array
  */
 public static function exec($command, $formatted = false, $prefix = '> ')
 {
     $commandOriginal = $command;
     $command = $command . ' 2>&1';
     // stifle output; we'll include it in the returned array
     $result = exec($command, $commandOutput, $exitCode);
     $output['raw'] = '';
     if ($formatted) {
         $output['formatted'] = '';
         $color = $exitCode == 0 ? 'green' : 'red';
     }
     foreach ($commandOutput as $line) {
         $output['raw'] .= $line . PHP_EOL;
         if ($formatted) {
             $output['formatted'] .= Output::colorize($prefix . $line, $color) . PHP_EOL;
         }
     }
     $output['raw'] = trim($output['raw']);
     return ['command' => $commandOriginal, 'output' => $output, 'exitCode' => $exitCode];
 }
示例#2
0
文件: Watch.php 项目: reisraff/phulp
 /**
  * @param Source $src
  * @param mixed $tasks
  * @param Phulp $phulp
  */
 public function __construct(Source $src, $tasks, Phulp $phulp)
 {
     $phulp->getLoop()->addPeriodicTimer(0.002, function () use($src, $tasks, $phulp) {
         foreach ($src->getDistFiles() as $distFile) {
             if (!empty($distFile->getFullpath()) && file_exists($distFile->getFullpath())) {
                 clearstatcache();
                 $timeChange = @filemtime(rtrim($distFile->getFullpath(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $distFile->getName());
                 if ($distFile->getLastChangeTime() < $timeChange) {
                     Output::out('[' . Output::colorize((new \DateTime())->format('H:i:s'), 'light_gray') . ']' . ' The file "' . Output::colorize(rtrim($distFile->getRelativePath(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $distFile->getName(), 'light_magenta') . '" was changed');
                     $distFile->setLastChangeTime($timeChange);
                     if (is_array($tasks)) {
                         $phulp->start($tasks);
                     } elseif (is_callable($tasks)) {
                         $tasks($phulp);
                     }
                 }
             }
         }
     });
 }