<?php

namespace app\util\sm;

class Exec
{
    protected static function dirPath(): string
    {
        return dirname(__FILE__) . DIRECTORY_SEPARATOR;
    }

    public static function exec(string $cmd): string
    {
        $output = null;
        $returnVal = null;

        $result = exec($cmd, $output, $returnVal);

        if (0 !== $returnVal) {
            throw new \LogicException('exec执行异常:' . $cmd);
        }
        if (!$result) {
            throw new \LogicException('exec执行返回值异常' . $cmd);
        }

        return trim($result);
    }

    public static function exec2(string $cmd)
    {
        $output = null;
        $returnVal = null;

        exec($cmd, $output, $returnVal);

        if (0 !== $returnVal) {
            throw new \LogicException('exec执行异常:' . $cmd);
        }
        if (is_array($output)) {
            if (count($output) == 2 && $output[1] !== 'success') {
                throw new \LogicException($output[1]);
            }
            return $output[0];
        }
        return $output;
    }

}