Beispiel #1
0
 /**
  * Register the singletons used in the application
  */
 protected function registerServices()
 {
     // register the app as a singleton
     self::$instance = $this;
     static::openLog('app.main');
     // Get container
     $container = $this->getContainer();
     $config = $this->config;
     // Register component on container
     $container['view'] = function ($container) {
         $view = new Twig(VIEWS_DIR, ['cache' => CACHE_DIR . '/views', 'auto_reload' => $this->debug]);
         $view->addExtension(new TwigExtension($container['router'], $container['request']->getUri()));
         return $view;
     };
     $container['elasticsearch'] = function ($container) use($config) {
         if (!empty($config['elasticsearch_user']) && !empty($config['elasticsearch_pass'])) {
             $host = sprintf('http://%s:%s@%s:%d', $config['elasticsearch_user'], $config['elasticsearch_pass'], $config['elasticsearch_host'], $config['elasticsearch_port']);
         } else {
             $host = sprintf('http://%s:%s', $config['elasticsearch_host'], $config['elasticsearch_port']);
         }
         // limit the hosts to one since we access it from a ELB
         $client = ClientBuilder::create()->setHosts([$host])->build();
         if (!Util::checkHostAvailability($config['elasticsearch_host'], $config['elasticsearch_port'])) {
             throw new \Exception('The Elasticsearch cluster seems to be unavailable.');
         }
         return $client;
     };
     /**
      * @return Config
      */
     $container['config'] = function () use($config) {
         return $config;
     };
 }
Beispiel #2
0
    /**
     * This method processes the config.ini file and caches it
     *
     * @param string $parametersFile full path to config.ini
     *
     * @return mixed|null
     * @throws Exception
     */
    public function processParameters($parametersFile)
    {
        $return = null;
        $parametersFilePath = CONFIG_DIR . '/' . $parametersFile;
        $cacheFile = $parametersFile . '_cache.php';
        $cacheFilePath = CACHE_DIR . '/' . $cacheFile;
        if (!is_file($parametersFilePath)) {
            throw new Exception(sprintf('Config file %s cannot be found!', $parametersFile));
        }
        if (is_file($cacheFilePath) && filemtime($parametersFilePath) == filemtime($cacheFilePath)) {
            $return = (include $cacheFilePath);
        } else {
            if (!is_writable(CACHE_DIR)) {
                throw new Exception(sprintf('Cache file %s could not be created at %s, please make sure it is writable.', $cacheFile, CACHE_DIR));
            }
            $parameters = @parse_ini_file($parametersFilePath);
            if (!$parameters) {
                throw new Exception(sprintf('Could not parse config file'));
            }
            // export the config and write it to the file
            $config = var_export($parameters, true);
            $date = date("Y-m-d h:i:s");
            $cacheContents = <<<EOD
    <?php

    /**
     * Generated with \\Hatchup\\App
     *
     * on {$date}
     */

    return {$config};
EOD;
            // put the parsed config in the cache file and modify the access
            // times on both the config and the cache file
            file_put_contents($cacheFilePath, $cacheContents);
            Util::touchFile($cacheFilePath);
            $return = (include $cacheFilePath);
        }
        return $return;
    }
Beispiel #3
0
 /**
  * @param $classFile
  *
  * @return string
  * @throws Exception
  */
 protected function processClass($classFile)
 {
     $content = file_get_contents($classFile);
     $result = '';
     preg_match_all('/class\\s+(\\w*)\\s*(extends\\s+)?([^{])*/s', $content, $mclass, PREG_SET_ORDER);
     $className = $mclass[0][1];
     if (!$className) {
         throw new Exception(sprintf('Class not found in %s', $classFile));
     }
     $refl = new ReflectionClass($this->baseNameSpace . '\\' . $className);
     $methods = $refl->getMethods();
     /** @var ReflectionMethod $method */
     foreach ($methods as $method) {
         if (strpos($method->getName(), 'Action')) {
             $doc = $method->getDocComment();
             preg_match_all("/\\*\\s+@([a-zA-z]+)\\s*\\('([^']*)'\\)/s", $doc, $annotations);
             $httpMethods = [];
             $route = '';
             foreach ($annotations[1] as $idx => $annotation) {
                 if (strtolower($annotation) == 'route') {
                     $route = $annotations[2][$idx];
                 } elseif (strtolower($annotation) == 'method') {
                     $httpMethods[] = $annotations[2][$idx];
                 }
             }
             if (count($httpMethods) == 0 || empty($route)) {
                 throw new Exception(sprintf('Method %s has invalid annotations, route or method invalid.', $method->getName()));
             }
             $result .= str_replace('\\', '\\\\', sprintf('$this->app->map(%s, "%s", "%s\\%s:%s");' . PHP_EOL, Util::shortExport($httpMethods), $route, $this->baseNameSpace, $className, $method->getName()));
         }
     }
     return $result;
 }