示例#1
0
 public static function cache($save = FALSE, $append = FALSE)
 {
     if ($save === TRUE) {
         try {
             JsonApiApplication::cache('Route::cache()', Route::$_routes);
         } catch (Exception $e) {
             throw new JsonApiApplication_Exception('One or more routes could not be cached (:message)', array(':message' => $e->getMessage()), 0, $e);
         }
     } else {
         if ($routes = JsonApiApplication::cache('Route::cache()')) {
             if ($append) {
                 Route::$_routes += $routes;
             } else {
                 Route::$_routes = $routes;
             }
             return Route::$cache = TRUE;
         } else {
             return Route::$cache = FALSE;
         }
     }
 }
示例#2
0
 public static function shutdown_handler()
 {
     if (!JsonApiApplication::$_init) {
         return;
     }
     try {
         if (JsonApiApplication::$caching === TRUE and JsonApiApplication::$_files_changed === TRUE) {
             // Write the file path cache
             JsonApiApplication::cache("JsonApiApplication::find_file()", JsonApiApplication::$_files);
         }
     } catch (Exception $e) {
         // Pass the exception to the handler
         JsonApiApplication_Exception::handler($e);
     }
     if (JsonApiApplication::$errors and $error = error_get_last() and in_array($error["type"], JsonApiApplication::$shutdown_errors)) {
         // Clean the output buffer
         ob_get_level() and ob_clean();
         // Fake an exception for nice debugging
         JsonApiApplication_Exception::handler(new ErrorException($error["message"], $error["type"], 0, $error["file"], $error["line"]));
         // Shutdown now to avoid a "death loop"
         exit(1);
     }
 }
示例#3
0
 public function execute($db = NULL, $as_object = NULL, $object_params = NULL)
 {
     if (!is_object($db)) {
         $db = Database::instance($db);
     }
     if ($as_object === NULL) {
         $as_object = $this->_as_object;
     }
     if ($object_params === NULL) {
         $object_params = $this->_object_params;
     }
     $sql = $this->compile($db);
     if ($this->_lifetime !== NULL and $this->_type === Database::SELECT) {
         $cache_key = 'Database::query("' . $db . '", "' . $sql . '")';
         if (($result = JsonApiApplication::cache($cache_key, NULL, $this->_lifetime)) !== NULL and !$this->_force_execute) {
             return new Database_Result_Cached($result, $sql, $as_object, $object_params);
         }
     }
     $result = $db->query($this->_type, $sql, $as_object, $object_params);
     if (isset($cache_key) and $this->_lifetime > 0) {
         JsonApiApplication::cache($cache_key, $result->as_array(), $this->_lifetime);
     }
     return $result;
 }
 /**
  * Gets the total application run time and memory usage. Caches the result
  * so that it can be compared between requests.
  *
  *     list($time, $memory) = Profiler::application();
  *
  * @return  array  execution time, memory
  * @uses    JsonApiApplication::cache
  */
 public static function application()
 {
     // Load the stats from cache, which is valid for 1 day
     $stats = JsonApiApplication::cache('profiler_application_stats', NULL, 3600 * 24);
     if (!is_array($stats) or $stats['count'] > Profiler::$rollover) {
         // Initialize the stats array
         $stats = array('min' => array('time' => NULL, 'memory' => NULL), 'max' => array('time' => NULL, 'memory' => NULL), 'total' => array('time' => NULL, 'memory' => NULL), 'count' => 0);
     }
     // Get the application run time
     $time = microtime(TRUE) - APPLICATION_START_TIME;
     // Get the total memory usage
     $memory = memory_get_usage() - APPLICATION_START_MEMORY;
     // Calculate max time
     if ($stats['max']['time'] === NULL or $time > $stats['max']['time']) {
         $stats['max']['time'] = $time;
     }
     // Calculate min time
     if ($stats['min']['time'] === NULL or $time < $stats['min']['time']) {
         $stats['min']['time'] = $time;
     }
     // Add to total time
     $stats['total']['time'] += $time;
     // Calculate max memory
     if ($stats['max']['memory'] === NULL or $memory > $stats['max']['memory']) {
         $stats['max']['memory'] = $memory;
     }
     // Calculate min memory
     if ($stats['min']['memory'] === NULL or $memory < $stats['min']['memory']) {
         $stats['min']['memory'] = $memory;
     }
     // Add to total memory
     $stats['total']['memory'] += $memory;
     // Another mark has been added to the stats
     $stats['count']++;
     // Determine the averages
     $stats['average'] = array('time' => $stats['total']['time'] / $stats['count'], 'memory' => $stats['total']['memory'] / $stats['count']);
     // Cache the new stats
     JsonApiApplication::cache('profiler_application_stats', $stats);
     // Set the current application execution time and memory
     // Do NOT cache these, they are specific to the current request only
     $stats['current']['time'] = $time;
     $stats['current']['memory'] = $memory;
     // Return the total application run time and memory usage
     return $stats;
 }