Exemplo n.º 1
0
 public function service($name)
 {
     $start = AetherTimer::getMicroTime();
     $memcache = new Memcache();
     $memcache->connect('localhost', 11211);
     $db = Config::getDb();
     $routeSearch = new RouteSearch();
     $cacheKey = "stops_";
     $filters = array();
     if (isset($_GET['term'])) {
         $query = toLower($_GET['term']);
         $filters['search'] = new MongoRegex("/^{$query}/i");
         $cacheKey .= $query;
     } elseif (isset($_GET['lat']) && isset($_GET['long'])) {
         $lat = $_GET['lat'];
         $long = $_GET['long'];
         $regex = '/^[0-9]+\\.[0-9]*$/';
         $cacheKey .= $lat . "," . $long;
         if (preg_match($regex, $lat) && preg_match($regex, $long)) {
             $filters['location'] = array('$near' => array((double) $lat, (double) $long));
         }
     }
     if (isset($_GET['from']) && !empty($_GET['from'])) {
         $from = toLower($_GET['from']);
         $filters['connectsFrom'] = $from;
     }
     $cacheKey .= r_implode(":", $filters);
     $result = $memcache->get($cacheKey);
     if ($result) {
         $result = unserialize($result);
     } else {
         // Find stops near me!!
         $db->routes->ensureIndex(array('search' => 1));
         $filters['active'] = true;
         $stops = $db->stops->find($filters);
         $result = array('stops' => array());
         while ($stop = $stops->getNext()) {
             $result['stops'][] = array('name' => $stop['name']);
         }
         $result['generated'] = time();
         $result['length'] = count($result['stops']);
         $memcache->set($cacheKey, serialize($result), false, 7200);
     }
     $time = AetherTimer::getMicroTime() - $start;
     $result['time'] = $time;
     if (isset($_GET['callback'])) {
         return new AetherJSONPResponse($result, $_GET['callback']);
     } else {
         return new AetherJSONResponse($result);
     }
 }
Exemplo n.º 2
0
 /**
  * Start Aether.
  * On start it will parse the projects configuration file,
  * it will try to match the presented http request to a rule
  * in the project configuration and create some overview
  * over which modules it will need to render once
  * a request to render them comes
  *
  * @access public
  * @return Aether
  * @param string $configPath Optional path to the configuration file for the project
  */
 public function __construct($configPath = false)
 {
     self::$aetherPath = pathinfo(__FILE__, PATHINFO_DIRNAME) . "/";
     spl_autoload_register(array('Aether', 'autoLoad'));
     $this->sl = new AetherServiceLocator();
     $this->sl->set('aetherPath', self::$aetherPath);
     // Initiate all required helper objects
     $parsedUrl = new AetherUrlParser();
     $parsedUrl->parseServerArray($_SERVER);
     $this->sl->set('parsedUrl', $parsedUrl);
     // Set autoloader
     // TODO Make this more uesable
     /**
      * Find config folder for project
      * By convention the config folder is always placed at
      * $project/config, while using getcwd() MUST return the
      * $project/www/ folder
      */
     $projectPath = preg_replace("/www\\/?\$/", "", getcwd());
     $this->sl->set("projectRoot", $projectPath);
     if (!defined("PROJECT_PATH")) {
         define("PROJECT_PATH", $projectPath);
     }
     $paths = array($configPath, $projectPath . 'config/autogenerated.config.xml', $projectPath . 'config/aether.config.xml');
     foreach ($paths as $configPath) {
         if (file_exists($configPath)) {
             break;
         }
     }
     try {
         $config = new AetherConfig($configPath);
         $config->matchUrl($parsedUrl);
         $this->sl->set('aetherConfig', $config);
     } catch (AetherMissingFileException $e) {
         /**
          * This means that someone forgot to ensure the config
          * file actually exists
          */
         $msg = "No configuration file for project found: " . $e->getMessage();
         throw new Exception($msg);
     } catch (AetherNoUrlRuleMatchException $e) {
         /**
          * This means parsing of configuration file failed
          * by the simple fact that no rules matches
          * the url. This is due to a bad developer
          */
         $msg = "No rule matched url in config file: " . $e->getMessage();
         throw new Exception($msg);
     }
     $options = $config->getOptions(array('AetherRunningMode' => 'prod', 'cache' => 'off'));
     if ($options['cache'] == 'on') {
         $cacheClass = isset($options['cacheClass']) ? $options['cacheClass'] : 'AetherCache';
         $cacheOptions = isset($options['cacheOptions']) ? $options['cacheOptions'] : [];
         $cache = $this->getCacheObject($cacheClass, $cacheOptions);
         $this->sl->set("cache", $cache);
     }
     /**
      * Make sure base and root for this request is stored
      * in the service locator so it can be made available
      * to the magical $aether array in templates
      */
     $magic = $this->sl->getVector('templateGlobals');
     $magic['base'] = $config->getBase();
     $magic['root'] = $config->getRoot();
     $magic['urlVars'] = $config->getUrlVars();
     $magic['runningMode'] = $options['AetherRunningMode'];
     $magic['requestUri'] = $_SERVER['REQUEST_URI'];
     $magic['domain'] = $_SERVER['HTTP_HOST'];
     if (isset($_SERVER['HTTP_REFERER'])) {
         $magic['referer'] = $_SERVER['HTTP_REFERER'];
     }
     $magic['options'] = $options;
     /**
      * If we are in TEST mode we should prepare a timer object
      * and time everything that happens
      */
     if ($options['AetherRunningMode'] == 'test') {
         // Prepare timer
         $timer = new AetherTimer();
         $timer->start('aether_main');
         $this->sl->set('timer', $timer);
     }
     // Initiate section
     try {
         $searchPath = isset($options['searchpath']) ? $options['searchpath'] : $projectPath;
         AetherSectionFactory::$path = $searchPath;
         $this->section = AetherSectionFactory::create($config->getSection(), $this->sl);
         $this->sl->set('section', $this->section);
         if (isset($timer)) {
             $timer->tick('aether_main', 'section_initiate');
         }
     } catch (Exception $e) {
         // Failed to load section, what to do?
         throw new Exception('Failed horribly: ' . $e->getMessage());
     }
 }
 private function performSearch($searcher, $data, $timer = false)
 {
     $from = $data['from'];
     $to = $data['to'];
     $time = $data['time'];
     $offset = $data['offset'];
     $limit = $data['limit'];
     $weekday = $data['weekday'];
     $sTime = AetherTimer::getMicroTime();
     $hits = $searcher->search($from, $to, $time, $weekday, $limit, $offset, $timer);
     $timeUsed = AetherTimer::getMicroTime() - $sTime;
     $db = Config::GetDb();
     $db->log->insert(array('hits' => $searcher->count, 'from' => $from, 'to' => $to, 'time' => time(), 'date' => date("Y-m-d H:i:s"), 'timeused' => $timeUsed, 'ua' => $_SERVER['HTTP_USER_AGENT'], 'ip' => $_SERVER['REMOTE_ADDR']));
     return $hits;
 }