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</>"); }
<?php use app\core\App; require_once '../app/Config.php'; require_once '../app/AutoLoad.php'; // default controller and its action define('DEFAULT_CONTROLLER', 'HomeController'); define('DEFAULT_ACTION', 'indexAction'); define('DEFAULT_ROUTE', 'home'); // db connection params define('DB_DRIVER', 'MySQLiDriver'); define("DB_CONN_PARAMS", serialize(array('host' => 'localhost', 'username' => 'root', 'password' => '', 'db' => 'snp'))); // adding routes to global Scope $app = new App(); $app->addRoute('home', 'Home', false); $app->addRoute('register', 'Register', false); $app->addRoute('login', 'Login', false); $app->addRoute('news', 'News', false); $app->addRoute('categories', 'Category', false); $app->addRoute('comments', 'Comment', false); $app->addRoute('logout', 'Logout', false); $app->run();
/** * 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(); } } } }
header("Cache-Control: no-transform"); define('DS', DIRECTORY_SEPARATOR); define('PS', PATH_SEPARATOR); define('BP', dirname(__FILE__)); define('SERVER_NAME', $_SERVER['SERVER_NAME']); require_once 'App/Core/App.php'; require_once 'App/Core/Autoloader.php'; $config = array('db' => array('host' => 'localhost', 'username' => 'root', 'password' => 'root', 'dbname' => 'test')); function show_error($heading, $message, $template = '') { //pr($traces); $msg = '<b>' . $heading . ' : </b><br/>' . $message . '<br/>'; /*$traces = debug_backtrace(); foreach ($traces[0] as $k => $v){ if(!is_object($v) && !is_array($v)){ if($k == 'file' || $k == 'line'){ $msg .= '<b>'.$k .'</b> => '.$v.'<br/>'; } } }*/ $res = array(); $res['error'] = $msg; echo json_encode($res); } function pr($t) { echo '<pre>'; print_r($t); } Core\App::run($config);
<?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); }