public function execute(InputInterface $input, OutputInterface $output) { $app = App::instance()->debug(true); $db = $app->service(Database::class); $basePath = $app->get('basePath'); $schemas = ['schema' => $basePath . 'app/schema/1-schema.sql', 'init' => $basePath . 'app/schema/2-init-data.sql', 'dummy' => $basePath . 'app/schema/3-dummy-data.sql']; if ($filter = $input->getArgument('schema')) { $schemaToImport = []; foreach ($filter as $key) { if (isset($schemas[$key])) { $schemaToImport[] = $schemas[$key]; } } } else { $schemaToImport = $schemas; } foreach ($schemaToImport as $schema) { $db->import($schema); if ($db->pdo()->errorCode() != '00000') { break; } $output->writeln(" - <fg=green>'{$schema}'</> imported", OutputInterface::VERBOSITY_VERBOSE); } $error = $db->pdo()->errorInfo(); if ($error[0] === '00000') { $count = count($schemaToImport); $output->writeln(" <fg=yellow>{$count} schema imported</>"); } else { $output->writeln("<error>\n({$error['1']}){$error['2']}\n</error>"); } }
public function execute(InputInterface $input, OutputInterface $output) { $db = App::instance()->service(Database::class); $db->create(); $error = $db->pdo()->errorInfo(); if ($error[0] === '00000') { $output->writeln(" <fg=yellow>Database created</>"); } else { $output->writeln("<error>\n({$error['1']}){$error['2']}\n</error>"); } }
protected function seed($table, $callback) { $tables = is_array($table) ? $table : explode(',', $table); $args = []; $app = App::instance(); foreach ($tables as $table) { $args[] = $app->service(BatchInsert::class, [$table]); } $this->tableCounter += count($tables); $this->recordInserted += (int) call_user_func_array($callback, $args); return $this; }
public function __construct() { $this->app = App::instance(); if (!$this->templatePath) { $this->templatePath = $this->app->get('templatePath'); } if (!$this->viewPath) { $fullClass = get_called_class(); $pos = strrpos($fullClass, '\\'); $nsp = false === $pos ? '/' : substr($fullClass, 0, $pos); $this->viewPath = Helper::fixSlashes(dirname(dirname(__DIR__)) . '/' . $nsp . '/view/'); } }
public function execute(InputInterface $input, OutputInterface $output) { // fill table list here $tables = ['user']; if ($filter = $input->getArgument('table')) { $tableToClear = []; foreach (explode(',', $filter) as $table) { if ($found = array_search($table, $tables)) { $tableToClear[] = $table; } } } else { $tableToClear = $tables; } $db = App::instance()->service(Database::class); $sql = ''; foreach ($tableToClear as $table) { $sql .= "delete from {$table};"; } $db->exec($sql); $count = count($tableToClear); $output->writeln(" <fg=yellow>{$count} table(s) cleared</>"); }
public function execute(InputInterface $input, OutputInterface $output) { $db = App::instance()->service(Database::class); $db->drop(); $output->writeln(" <fg=yellow>Database dropped</>"); }
/** * Log query if debug active * @param string $sql * @param array $params * @param array $error */ public function log($sql, array $params, array $error) { $app = App::instance(); if ($app->debug()) { $no = -1; $params = array_merge($params, []); $this->logs[] = preg_replace_callback('/(?<qm>\\?)|(?<p>:\\w+)/', function ($match) use(&$no, $params) { $no++; if (isset($match['qm']) && isset($params[$no])) { return "'" . $params[$no] . "'"; } elseif (isset($match['p']) && isset($params[$match['p']])) { return "'" . $params[$match['p']] . "'"; } else { return isset($match['qm']) ? '?' : ':' . $match['p']; } }, $sql); if ('00000' !== $error[0]) { $this->errors[] = $error; if (!$app->get('continueOnDBError')) { $this->dumpError(); } } } }
<?php use app\Model; use app\core\App; use app\core\Breadcrumb; use app\core\Database; use app\core\Loader; // require loader require __DIR__ . '/core/Loader.php'; $loader = new Loader(); $loader->add(dirname(__DIR__))->register(); // instantiate main class $app = App::instance(); // register configuration from file // it will remains in App properties $config = $app->load(__DIR__ . '/config/config.php'); $app->register($config); // configure services $database = $app->load(__DIR__ . '/config/database.php'); $rules = [Database::class => ['constructParams' => [$database], 'shared' => true], Model::class => ['shared' => true]]; $service = $app->service(); foreach ($rules as $key => $value) { $service->addRule($key, $value); }