public function __construct()
 {
     $userConf = Config::getAll('twig');
     $defaultConf = ['template_path' => RESOURCES_PATH . 'views', 'cache_path' => STORAGE_PATH . 'cache', 'use_cache' => true];
     $config = array_merge($defaultConf, $userConf);
     $loader = new \Twig_Loader_Filesystem($config['template_path']);
     $this->twig = new \Twig_Environment($loader, array('cache' => $config['use_cache'] ? STORAGE_PATH . 'cache' : false, "auto_reload" => true));
     foreach (self::$extensions as $ext) {
         $this->twig->addExtension($ext);
     }
 }
Exemplo n.º 2
0
 public function __construct()
 {
     $provider = Config::get('providers', 'view', null);
     if ($provider) {
         // use reflection to detect the provider class name
         $refMethod = new \ReflectionMethod($provider, 'provides');
         $rendererClass = $refMethod->invoke(new $provider());
         if (empty($rendererClass)) {
             throw new ViewException("Class {$provider} doesn't provides a valid renderer class");
         }
         $this->renderer = new $rendererClass();
     } else {
         $refMethod = new \ReflectionMethod(__NAMESPACE__ . '\\ViewProvider', 'provides');
         $rendererClass = $refMethod->invoke(new ViewProvider());
         $this->renderer = new $rendererClass();
     }
 }
Exemplo n.º 3
0
 public static function create($table, \Closure $fn)
 {
     $bp = new Blueprint($table);
     $fn($bp);
     $connectionName = 'default';
     $trace = debug_backtrace();
     if (isset($trace[1]) && isset($trace[1]['object'])) {
         $clazz = get_class($trace[1]['object']);
         if ($clazz != null && class_exists($clazz)) {
             $ref = new \ReflectionMethod($clazz, 'getConnection');
             $attribute = $ref->invoke($trace[1]['object']);
             if ($attribute) {
                 $connectionName = $attribute;
             }
         }
     }
     $conf = Config::getAll('database');
     if (!isset($conf[$connectionName])) {
         throw new IllegalArgumentException('Unknown database connection name: ' . $connectionName);
     }
     $bp->build(new Connection($conf[$connectionName]));
 }
 private function startSession()
 {
     if (headers_sent()) {
         throw new SessionException('Unable to start session handling, headers already sent.');
     }
     $provider = Config::get('providers.session');
     if ($provider) {
         if (!class_exists($provider)) {
             throw new IllegalArgumentException('Unable to set session provider to ' . $provider);
         }
         $handlerClass = (new $provider())->provides();
         $handler = new $handlerClass();
         session_set_save_handler($handler, true);
     }
     session_start();
 }
Exemplo n.º 5
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('start migrating...');
     $migrations_path = APP_PATH . 'database' . DIRECTORY_SEPARATOR . 'migrations';
     if (!file_exists($migrations_path)) {
         // if dir doesn't exists, nothing is to migrate
         return;
     }
     // get (default) database connection
     $conf = Config::getAll('database');
     $default_config = isset($conf['default']) ? $conf['default'] : null;
     if ($default_config == null) {
         throw new ConnectionException('No default connection set');
     }
     $conn = new Connection($default_config);
     // find migrations table. if something is going wrong
     // a exception is triggered
     $migrations = $conn->getDatabaseSchema()->table('migrations');
     if ($migrations == null) {
         $migrations = new Blueprint('migrations');
         $migrations->increments('id')->string('name')->unique()->timestamps()->primary('id');
         // create table migrations
         // if something is going wrong a exception is triggered
         $migrations->build($conn);
     }
     $files = array();
     if ($handle = opendir($migrations_path)) {
         while (false !== ($entry = readdir($handle))) {
             if ($entry != "." && $entry != ".." && !is_dir($entry)) {
                 if (endsWith($entry, ".php")) {
                     $basename = basename($entry, '.php');
                     $ret = $conn->raw("select name from `migrations` where name='{$basename}' limit 1");
                     if ($ret != null && $ret->rowCount() == 1) {
                         continue;
                     }
                     $files[] = $migrations_path . DIRECTORY_SEPARATOR . $entry;
                 }
             }
         }
         closedir($handle);
     } else {
         return;
     }
     foreach ($files as $file) {
         $content = file_get_contents($file);
         $lines = explode(PHP_EOL, $content);
         foreach ($lines as $line) {
             if (preg_match('/class[\\s]+([a-zA-Z]+)/', $line, $matches)) {
                 include $file;
                 $clazz = $matches[1];
                 if (!class_exists($clazz)) {
                     throw new IllegalArgumentException("Class {$clazz} doesn't exists");
                 }
                 $instance = new $clazz();
                 if (!$instance instanceof MigrateInterface) {
                     throw new IllegalArgumentException("Class {$clazz} doesn't implements MigrateInterface");
                 }
                 $instance->up();
                 // register migration in database
                 $conn->raw('insert into migrations (name, created_at) VALUES ("' . basename($file, '.php') . '", null)');
                 $output->writeln('Migrated table class ' . $clazz);
             }
         }
     }
     $output->writeln('finished migrating...');
 }
Exemplo n.º 6
0
 public function delete()
 {
     $table_name = $this->getTable();
     $config = Config::get('database', $this->getConnection(), 'default');
     $conn = new Connection($config);
     $pk = $this->getPrimaryKey();
     if (!isset($this->attributes[$pk])) {
         return -1;
     }
     return (new DeleteQuery($table_name, $conn))->where($pk, $this->attributes[$pk])->execute();
 }