/** * Bootstrap a web app. * * The App Folder provided must be relative to the current working directory, or * absolute. The Config File Name is a base name (e.g. `config`) located in the * `config` folder inside the App Folder (e.g. `app/config/config.php`). * * @param string $app_folder The path to the app folder * @param string $config_file_name Base name of the file to use for configuration. */ public function __construct($app_folder, $config_file_name = 'config') { $this->container = (require __DIR__ . '/config/bootstrap.php'); $app_path_pre = getcwd() . DIRECTORY_SEPARATOR . $app_folder; $app_path = realpath($app_path_pre); if ($app_path === false) { throw new \InvalidArgumentException("The app path does not exist: {$app_path_pre}"); } $this->container['app_path'] = $app_path; $this->container['config_folder'] = $config_file_name; # init the pew() helper pew(null, $this->container); # import app config and services $this->loadAppConfig("{$app_path}/config/{$config_file_name}.php"); $this->loadAppBootstrap(); }
/** * Helper for flash data. * * @param string $key Flash data key to read * @param mixed $default Value to return in case the keys don't exist * @return mixed Value of the key */ function flash($key = null, $default = null) { static $session; if (!$session) { $session = pew('session'); } if (null === $key) { return $session->getFlashBag()->all(); } elseif ($session->getFlashBag()->has($key)) { return $session->getFlashBag()->get($key); } return $default; }
/** * Initialize a model binding it to a database table. * * @param string $table Name of the table * @param Database $db Database instance to use */ public function init($table = null, Database $db = null) { # get the Database class instance $this->db = is_null($db) ? pew('db') : $db; $this->table = $table ?: $this->tableName(); if (!$this->db->table_exists($this->table)) { throw new TableNotFoundException("Table {$this->table} for model " . get_class($this) . " not found."); } # some metadata about the table $primary_key = $this->db->get_pk($this->table); $columns = $this->db->get_cols($this->table); $this->tableData['name'] = $this->table; $this->tableData['primary_key'] = $primary_key; $this->tableData['columns'] = $columns; $this->tableData['column_names'] = array_combine($columns, array_fill(0, count($columns), null)); }