Exemplo n.º 1
0
 /**
  * Returns the full path of the file.
  *
  * @return string Full path to the file
  */
 public function pwd()
 {
     if ($this->path === null) {
         $dir = $this->Folder->pwd();
         if (is_dir($dir)) {
             $this->path = $this->Folder->slashTerm($dir) . $this->name;
         }
     }
     return $this->path;
 }
Exemplo n.º 2
0
 /**
  * Process the tasks when they need to run
  *
  * @access private
  * @return void
  */
 private function runjobs()
 {
     $dir = new Folder(TMP);
     // set processing flag so function takes place only once at any given time
     $processing = count($dir->find('\\.scheduler_running_flag'));
     $processingFlag = new File($dir->slashTerm($dir->pwd()) . '.scheduler_running_flag');
     if ($processing && time() - $processingFlag->lastChange() < $this->processingTimeout) {
         $this->out("Scheduler already running! Exiting.");
         return false;
     } else {
         $processingFlag->delete();
         $processingFlag->create();
     }
     if (!$this->storePath) {
         $this->storePath = TMP;
     }
     // look for a store of the previous run
     $store = "";
     $storeFilePath = $this->storePath . $this->storeFile;
     if (file_exists($storeFilePath)) {
         $store = file_get_contents($storeFilePath);
     }
     $this->out('Reading from: ' . $storeFilePath);
     // build or rebuild the store
     if ($store != '') {
         $store = json_decode($store, true);
     } else {
         $store = $this->schedule;
     }
     // run the jobs that need to be run, record the time
     foreach ($this->schedule as $name => $job) {
         $now = new DateTime();
         $task = $job['task'];
         $action = $job['action'];
         // if the job has never been run before, create it
         if (!isset($store[$name])) {
             $store[$name] = $job;
         }
         // figure out the last run date
         $tmptime = $store[$name]['lastRun'];
         if ($tmptime == null) {
             $tmptime = new DateTime("1969-01-01 00:00:00");
         } elseif (is_array($tmptime)) {
             $tmptime = new DateTime($tmptime['date'], new DateTimeZone($tmptime['timezone']));
         } elseif (is_string($tmptime)) {
             $tmptime = new DateTime($tmptime);
         }
         // determine the next run time based on the last
         if (substr($job['interval'], 0, 1) === 'P') {
             $tmptime->add(new DateInterval($job['interval']));
             // "P10DT4H" http://www.php.net/manual/en/class.dateinterval.php
         } else {
             $tmptime->modify($job['interval']);
             // "next day 10:30" http://www.php.net/manual/en/datetime.formats.relative.php
         }
         // is it time to run? has it never been run before?
         if ($tmptime <= $now) {
             $this->hr();
             $this->out("Running {$name}");
             $this->hr();
             if (!isset($this->{$task})) {
                 $this->{$task} = $this->Tasks->load($task);
                 // load models if they aren't already
                 // foreach ($this->$task->uses as $mk => $mv) {
                 // 	if (!isset($this->$task->$mv)) {
                 // 		App::uses('AppModel', 'Model');
                 // 		App::uses($mv, 'Model');
                 // 		$this->$task->$mv = new $mv();
                 // 	}
                 // }
             }
             // grab the entire schedule record incase it was updated..
             $store[$name] = $this->schedule[$name];
             // execute the task and store the result
             $store[$name]['lastResult'] = call_user_func_array(array($this->{$task}, $action), $job['args']);
             // assign it the current time
             $now = new DateTime();
             $store[$name]['lastRun'] = $now->format('Y-m-d H:i:s');
         }
     }
     // write the store back to the file
     file_put_contents($this->storePath . $this->storeFile, json_encode($store));
     // remove processing flag
     $processingFlag->delete();
 }
Exemplo n.º 3
0
 /**
  * Get the real path (taking ".." and such into account)
  *
  * @param string $path Path to resolve
  * @return string The resolved path
  */
 public function realpath($path)
 {
     if (strpos($path, '..') === false) {
         if (!Folder::isAbsolute($path)) {
             $path = Folder::addPathElement($this->path, $path);
         }
         return $path;
     }
     $path = str_replace('/', DIRECTORY_SEPARATOR, trim($path));
     $parts = explode(DIRECTORY_SEPARATOR, $path);
     $newparts = [];
     $newpath = '';
     if ($path[0] === DIRECTORY_SEPARATOR) {
         $newpath = DIRECTORY_SEPARATOR;
     }
     while (($part = array_shift($parts)) !== null) {
         if ($part === '.' || $part === '') {
             continue;
         }
         if ($part === '..') {
             if (!empty($newparts)) {
                 array_pop($newparts);
                 continue;
             }
             return false;
         }
         $newparts[] = $part;
     }
     $newpath .= implode(DIRECTORY_SEPARATOR, $newparts);
     return Folder::slashTerm($newpath);
 }
Exemplo n.º 4
0
 /**
  * testStatic method
  *
  * @return void
  */
 public function testSlashTerm()
 {
     $result = Folder::slashTerm('/path/to/file');
     $this->assertEquals('/path/to/file/', $result);
 }