Pimcore\Tool\Console::exec PHP Метод

exec() публичный статический Метод

public static exec ( $cmd, null $outputFile = null, null $timeout = null ) : string
$cmd
$outputFile null
$timeout null
Результат string
    public static function exec($cmd, $outputFile = null, $timeout = null)
    {
        if ($timeout && self::getTimeoutBinary()) {
            // check if --kill-after flag is supported in timeout
            if (self::$timeoutKillAfterSupport === null) {
                $out = self::exec(self::getTimeoutBinary() . " --help");
                if (strpos($out, "--kill-after")) {
                    self::$timeoutKillAfterSupport = true;
                } else {
                    self::$timeoutKillAfterSupport = false;
                }
            }
            $killAfter = "";
            if (self::$timeoutKillAfterSupport) {
                $killAfter = " -k 1m";
            }
            $cmd = self::getTimeoutBinary() . $killAfter . " " . $timeout . "s " . $cmd;
        } elseif ($timeout) {
            Logger::warn("timeout binary not found, executing command without timeout");
        }
        if ($outputFile) {
            $cmd = $cmd . " > " . $outputFile . " 2>&1";
        } else {
            // send stderr to /dev/null otherwise this goes to the apache error log and can fill it up pretty quickly
            if (self::getSystemEnvironment() != 'windows') {
                $cmd .= " 2> /dev/null";
            }
        }
        Logger::debug("Executing command `" . $cmd . "` on the current shell");
        $return = shell_exec($cmd);
        return $return;
    }

Usage Example

Пример #1
0
 /**
  * @param $path
  */
 public static function optimize($path)
 {
     $format = getimagesize($path);
     if (is_array($format) && array_key_exists("mime", $format)) {
         $format = strtolower(str_replace("image/", "", $format["mime"]));
         if ($format == "png") {
             $optimizer = self::getPngOptimizerCli();
             if ($optimizer) {
                 /*if($optimizer["type"] == "pngquant") {
                                         Console::exec($optimizer["path"] . " --ext xxxoptimized.png " . $path, null, 60);
                                         $newFile = preg_replace("/\.png$/", "", $path);
                                         $newFile .= "xxxoptimized.png";
                 
                                         if(file_exists($newFile)) {
                                             unlink($path);
                                             rename($newFile, $path);
                                         }
                                     } else */
                 if ($optimizer["type"] == "pngcrush") {
                     $newFile = $path . ".xxxoptimized";
                     Console::exec($optimizer["path"] . " " . $path . " " . $newFile, null, 60);
                     if (file_exists($newFile)) {
                         unlink($path);
                         rename($newFile, $path);
                     }
                 }
             }
         } else {
             if ($format == "jpeg") {
                 $optimizer = self::getJpegOptimizerCli();
                 if ($optimizer) {
                     if ($optimizer["type"] == "imgmin") {
                         $newFile = $path . ".xxxoptimized";
                         Console::exec($optimizer["path"] . " " . $path . " " . $newFile, null, 60);
                         if (file_exists($newFile)) {
                             unlink($path);
                             rename($newFile, $path);
                         }
                     } else {
                         if ($optimizer["type"] == "jpegoptim") {
                             $additionalParams = "";
                             if (filesize($path) > 10000) {
                                 $additionalParams = " --all-progressive";
                             }
                             Console::exec($optimizer["path"] . $additionalParams . " -o --strip-all --max=85 " . $path, null, 60);
                         }
                     }
                 }
             }
         }
     }
 }
All Usage Examples Of Pimcore\Tool\Console::exec