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); } }
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])); }
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...'); }