<commit_before><?php
/**
 * Init
 * @author <NAME> <<EMAIL>> @mosesngone
 */

namespace BlueRidge;

use \Slim\Middleware;

class Init extends Middleware
{
    /**
     * Call
     * Middleware call
     */
    public function call(){

        $this->setApplicationEnvironment();    
        $this->next->call();
    }

    /**
     * Setup Application
     */
    protected function setApplicationEnvironment(){ 

        $app_env = getenv('APPLICATION_ENV');
        $appEnv= ($app_env)?$app_env:"production";  
        $configs = $this->getConfigs($appEnv); 
        
        $appSettings = ["mode"=>$appEnv,"debug"=>$configs->debug,"log.level"=>$configs->log_enabled];
        $this->app->config($appSettings);
        
        if($configs->database){
            $this->app->database = $this->setDbAdapter($configs->database);
        }

        if($configs->cache){
            $this->app->cache = $this->setCache($configs->cache);
        }

        if($configs->providers){
            $this->app->providers = $configs->providers;
        }

        $this->app->resource = $this->setResource();
    }

    /**
     * Get Application Settings
     */
    protected function getConfigs($appEnv){

        $config_file = "../api/configs/{$appEnv}.json";
        $config_content= file_get_contents($config_file); 
        return $configs =json_decode($config_content);   

    }

    /**
     * Setup Database Adapter
     */
    protected function setDbAdapter($db){
        $connection_url = "mongodb://{$db->host}:{$db->port}/{$db->name}";   
        if(isset($db->user) && isset($db->passwd)){
            $connection_url = "mongodb://{$db->user}:{$db->passwd}@{$db->host}:{$db->port}/{$db->name}";
        }

        $client= new \MongoClient($connection_url);
        return $client->selectDB($db->name);
    }

    /**
     * Setup Cache
     */
    protected function setCache(){

        $memcache = new \Memcache('localhost', 11211);
        return $memcache;

    }

    /**
     * Setup Routes and Requested Resource
     */
    protected function setResource(){

        $path = $this->app->request()->getPath();
        list($separator,$root,$base) = explode('/', $path) + [null,null,null];

        $jsonRoutes= file_get_contents("../api/configs/routes.json");    
        $routes =json_decode($jsonRoutes); 

        if(!array_key_exists($base, $routes)){
           return;
       }<commit_msg>chained routes to entities only<commit_after><?php
/**
 * Init
 * @author <NAME> <<EMAIL>> @mosesngone
 */

namespace BlueRidge;

use \Slim\Middleware;

class Init extends Middleware
{
    /**
     * Call
     * Middleware call
     */
    public function call(){

        $this->setApplicationEnvironment();    
        $this->next->call();
    }

    /**
     * Setup Application
     */
    protected function setApplicationEnvironment(){ 

        $app_env = getenv('APPLICATION_ENV');
        $appEnv= ($app_env)?$app_env:"production";  
        $configs = $this->getConfigs($appEnv); 
        
        $appSettings = ["mode"=>$appEnv,"debug"=>$configs->debug,"log.level"=>$configs->log_enabled];
        $this->app->config($appSettings);
        
        if($configs->database){
            $this->app->database = $this->setDbAdapter($configs->database);
        }

        if($configs->cache){
            $this->app->cache = $this->setCache($configs->cache);
        }

        if($configs->providers){
            $this->app->providers = $configs->providers;
        }

        $this->app->resource = $this->setResource();
    }

    /**
     * Get Application Settings
     */
    protected function getConfigs($appEnv){

        $config_file = "../api/configs/{$appEnv}.json";
        $config_content= file_get_contents($config_file); 
        return $configs =json_decode($config_content);   

    }

    /**
     * Setup Database Adapter
     */
    protected function setDbAdapter($db){
        $connection_url = "mongodb://{$db->host}:{$db->port}/{$db->name}";   
        if(isset($db->user) && isset($db->passwd)){
            $connection_url = "mongodb://{$db->user}:{$db->passwd}@{$db->host}:{$db->port}/{$db->name}";
        }

        $client= new \MongoClient($connection_url);
        return $client->selectDB($db->name);
    }

    /**
     * Setup Cache
     */
    protected function setCache(){

        $memcache = new \Memcache('localhost', 11211);
        return $memcache;

    }

    /**
     * Setup Routes and Requested Resource
     */
    protected function setResource(){

        $path = $this->app->request()->getPath();
        list($separator,$root,$base) = explode('/', $path) + [null,null,null];

        $jsonRoutes= file_get_contents("../api/configs/entity-routes.json");    
        $routes =json_decode($jsonRoutes); 

        if(!array_key_exists($base, $routes)){
           return;
       }
{'fraction_non_alphanumeric': 0.17865237997115185, 'fraction_numerical': 0.0020605810838656502, 'mean_word_length': 2.320109439124487, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3189126', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1460, 'n_words': 222}