Exemple #1
0
function gocron($file, $dir)
{
    if (!is_dir($dir)) {
        mkdir($dir);
    }
    /*
        * * * * *
        | | | | |
        | | | | +----- Дни недели (0-6), 0 - воскресенье
        | | | +------- Месяцы (1-12)
        | | +--------- Дни месяца (1-31)
        | +----------- Часы (0-23)
        +------------- Минуты (0-59)
    */
    // * - любое значение
    // 1 - определенное значение
    // 1-2 - интервал значений
    // 1,4 - список значений
    // */2 - четные значения
    // 1,2-3,*/4 - mix
    if (is_file($file)) {
        $crons = nsplit(template($file));
    } else {
        _log(__FUNCTION__ . ": INVALID CRON! | {$file}", E_USER_ERROR);
    }
    $time = func_num_args() > 2 ? func_get_arg(2) : time();
    $trigger = function ($cron) use($time) {
        $format = "iHdnw";
        for ($i = 0; $i < strlen($format); $i++) {
            $t = intval(date($format[$i], $time));
            foreach (explode(',', $cron[$i]) as $elem) {
                if ($elem === '*') {
                    continue 2;
                }
                if (is_numeric($elem) and $elem == $t) {
                    continue 2;
                }
                if (preg_match('~^(\\d+)-(\\d+)$~', $elem, $match) and intval($match[1]) <= $t and $t <= intval($match[2])) {
                    continue 2;
                }
                if (preg_match('~^\\*/(\\d+)$~', $elem, $match) and $t % $match[1] === 0) {
                    continue 2;
                }
            }
            return false;
        }
        return true;
    };
    foreach ($crons as $cron) {
        if (strpos($cron, '#') === 0) {
            continue;
        }
        $cron = preg_split('~\\s+~', $cron, 6);
        if (count($cron) != 6) {
            continue;
        }
        $exec = array_pop($cron);
        if (!$trigger($cron)) {
            continue;
        }
        $name = preg_split('~\\s+~', $exec);
        array_walk($name, function (&$_) {
            if (is_file($_)) {
                $_ = basename($_);
            }
            if (is_dir(dirname($_))) {
                $_ = basename($_);
            }
        });
        $name = implode(' ', $name);
        $std = sprintf("%s/%s.%s.txt", $dir, time(), substr(str_replace(' ', '-', normEn($name)), 0, 255));
        shell_exec($_ = sprintf("nohup bash -c %s >%s 2>&1 &", escapeshellarg($exec), escapeshellarg($std)));
        _log(__FUNCTION__ . ': ' . $_);
    }
}
Exemple #2
0
 public function testNorm()
 {
     $this->assertTrue(normLatin('ÁΓă') === "AGa");
     $this->assertTrue(normLatin('ђÜẽ') === "djUe");
     $this->assertTrue(normLatin('Màl Śir') === "Mal Sir");
     //
     $this->assertTrue(normLatinRu('привет мир') === "privet mir");
     $this->assertTrue(normLatinRu('щука ямка хрен') === "shchuka iamka khren");
     //
     $this->assertTrue(normSpace(" \t \n Привет \t \n мир! \t \n ") === "Привет мир!");
     $this->assertTrue(normTrim(" \t \n Привет \t \n мир! \t \n ") === "Привет \t \n мир!");
     //
     $this->assertTrue(normEn("Hello, world!") === "hello world");
     $this->assertTrue(normRu("Привет, мир!") === "привет мир");
 }