Advertisement
lemb

Untitled

Nov 28th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.80 KB | None | 0 0
  1. <?php
  2.  
  3. class Cron
  4. {
  5.     private $fileName;
  6.  
  7.     protected static $instance;
  8.  
  9.     public static function init($fileName = 'cache_cron')
  10.     {
  11.         if(!self::$instance) {
  12.             self::$instance = new self( $fileName );
  13.         }
  14.  
  15.         return self::$instance;
  16.     }
  17.  
  18.     private function __construct($fileName)
  19.     {
  20.         $this->fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName;
  21.     }
  22.  
  23.     private function __clone() {}
  24.  
  25.     public function runEvery($time)
  26.     {
  27.         if( !(file_exists($this->fileName) && ((filemtime($this->fileName) + $time) >= time())) ) {
  28.             $this->setExecuted();
  29.  
  30.             return true;
  31.         }
  32.  
  33.         return false;
  34.     }
  35.  
  36.     public function setExecuted()
  37.     {
  38.         touch($this->fileName);
  39.     }
  40. }
  41.  
  42. // 5 - кол секунд для повторного выполнения
  43. if( Cron::init()->runEvery(5) ) {
  44.     var_dump('Run baby run');
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement