/** * Register module * * @param Application $application * @throws Exception * @return Module */ public function register(Application $application) { parent::register($application); // Add route params for the controllers if (null !== $this->application->router()) { $this->application->router()->addControllerParams('*', ['application' => $this->application, 'console' => new \Pop\Console\Console(120, ' ')]); } if (!empty($this->application->config()['database']) && !empty($this->application->config()['database']['adapter'])) { $adapter = $this->application->config()['database']['adapter']; $options = ['database' => $this->application->config()['database']['database'], 'username' => $this->application->config()['database']['username'], 'password' => $this->application->config()['database']['password'], 'host' => $this->application->config()['database']['host'], 'type' => $this->application->config()['database']['type']]; $check = \Pop\Db\Db::check($adapter, $options); if (null !== $check) { throw new Exception('DB ' . $check); } $this->application->services()->set('database', ['call' => 'Pop\\Db\\Db::connect', 'params' => ['adapter' => $adapter, 'options' => $options]]); } if ($this->application->services()->isAvailable('database')) { Record::setDb($this->application->getService('database')); } // Set up triggers to check the application session $this->application->on('app.route.pre', function () { if (isset($_SERVER['argv'][1])) { echo PHP_EOL . ' App Console' . PHP_EOL; echo ' ===========' . PHP_EOL . PHP_EOL; } }, 1000); $this->application->on('app.dispatch.post', function () { echo PHP_EOL; }, 1000); return $this; }
/** * Register module * * @param Application $application * @throws Exception * @return Module */ public function register(Application $application) { parent::register($application); if (null !== $this->application->router()) { $this->application->router()->addControllerParams('*', ['application' => $this->application, 'request' => new Request(), 'response' => new Response()]); } if (!empty($this->application->config()['database']) && !empty($this->application->config()['database']['adapter'])) { $adapter = $this->application->config()['database']['adapter']; $options = ['database' => $this->application->config()['database']['database'], 'username' => $this->application->config()['database']['username'], 'password' => $this->application->config()['database']['password'], 'host' => $this->application->config()['database']['host'], 'type' => $this->application->config()['database']['type']]; $check = \Pop\Db\Db::check($adapter, $options); if (null !== $check) { throw new Exception('DB ' . $check); } $this->application->services()->set('database', ['call' => 'Pop\\Db\\Db::connect', 'params' => ['adapter' => $adapter, 'options' => $options]]); } if ($this->application->services()->isAvailable('database')) { Record::setDb($this->application->getService('database')); } if (isset($this->config['forms'])) { $this->application->mergeConfig(['forms' => $this->config['forms']]); } if (isset($this->config['resources'])) { $this->application->mergeConfig(['resources' => $this->config['resources']]); } $this->application->on('app.route.pre', 'App\\Event\\Ssl::check', 1000)->on('app.dispatch.pre', 'App\\Event\\Session::check', 1001)->on('app.dispatch.pre', 'App\\Event\\Acl::check', 1000); $this->initNav(); return $this; }
/** * Set the field values * * @param array $values * @return Install */ public function setFieldValues(array $values = null) { parent::setFieldValues($values); if ($_POST && !empty($this->db_adapter)) { // If not SQLite, check the DB parameters if (stripos($this->db_adapter, 'sqlite') === false) { $this->getElement('db_name')->addValidator(new Validator\NotEmpty(null, 'The database name is required.')); $this->getElement('db_username')->addValidator(new Validator\NotEmpty(null, 'The database username is required.')); $this->getElement('db_password')->addValidator(new Validator\NotEmpty(null, 'The database password is required.')); $this->getElement('db_host')->addValidator(new Validator\NotEmpty(null, 'The database host is required.')); } // Check the content path if (!$this->checkContentPath()) { $this->getElement('content_path')->addValidator(new Validator\NotEqual($this->content_path, wordwrap('The content directory (or subdirectories) either do not exist or are not writable.', 50, '<br />'))); } // Check the database credentials if ($this->isValid() && stripos($this->db_adapter, 'sqlite') === false) { if (stripos($this->db_adapter, 'pdo_') !== false) { $adapter = 'Pdo'; $type = str_replace('pdo_', '', strtolower($this->db_adapter)); } else { $adapter = ucfirst(strtolower($this->db_adapter)); $type = null; } $oldError = ini_get('error_reporting'); error_reporting(E_ERROR); $creds = ['database' => $this->db_name, 'username' => $this->db_username, 'password' => $this->db_password, 'host' => $this->db_host, 'type' => $type]; $dbCheck = Db::check($creds, $adapter); // If there is a DB error if (null != $dbCheck) { $this->getElement('db_adapter')->addValidator(new Validator\NotEqual($this->db_adapter, wordwrap($dbCheck, 50, '<br />'))); } else { $db = Db::connect($adapter, $creds); $version = $db->version(); $version = substr($version, strrpos($version, ' ') + 1); if (strpos($version, '-') !== false) { $version = substr($version, 0, strpos($version, '-')); } if (stripos($this->db_adapter, 'mysql') !== false && version_compare($version, '5.0') < 0) { $this->getElement('db_adapter')->addValidator(new Validator\NotEqual($this->db_adapter, 'The MySQL version must be 5.0 or greater.')); } else { if (stripos($this->db_adapter, 'pgsql') !== false && version_compare($version, '9.0') < 0) { $this->getElement('db_adapter')->addValidator(new Validator\NotEqual($this->db_adapter, 'The PostgreSQL version must be 9.0 or greater.')); } } } error_reporting($oldError); } } return $this; }
/** * Composer install method * * @param \Composer\Script\Event $event * @throws \Pop\Db\Exception * @return void */ public static function install($event) { $console = new Console(100, ' '); if (!file_exists(__DIR__ . '/../../data')) { mkdir(__DIR__ . '/../../data'); } chmod(__DIR__ . '/../../data', 0777); if (!file_exists(__DIR__ . '/../../app/config/application.php')) { $console->write(); $console->write($console->colorize('A configuration file was not detected.', Console::BOLD_YELLOW)); $console->write(); $createConfig = $console->prompt('Would you like to create one and install the database? [Y/N] ', ['y', 'n']); if (strtolower($createConfig) == 'y') { $console->write(); // Configure application database $dbName = ''; $dbUser = ''; $dbPass = ''; $dbHost = ''; $dbPrefix = ''; $dbAdapters = self::getDbAdapters(); $adapters = array_keys($dbAdapters); $dbChoices = []; $dsn = null; $i = 1; foreach ($dbAdapters as $a) { $console->write($i . ': ' . $a); $dbChoices[] = $i; $i++; } $console->write(); $adapter = $console->prompt('Please select one of the above database adapters: ', $dbChoices); $console->write(); // If PDO if (strpos($adapters[$adapter - 1], 'pdo') !== false) { $console->write('1: mysql'); $console->write('2: pgsql'); $console->write('3: sqlite'); $console->write(); $dsn = $console->prompt('Please select the PDO DSN: ', [1, 2, 3]); $dbInterface = 'Pdo'; $dbType = str_replace('pdo_', '', strtolower($adapters[$adapter - 1])); $console->write(); } else { $dbInterface = ucfirst(strtolower($adapters[$adapter - 1])); $dbType = null; } // If SQLite if ($dsn == 3 || $adapters[$adapter - 1] == 'sqlite') { if (!file_exists(__DIR__ . '/../../data/.htpop.sqlite')) { touch(__DIR__ . '/../../data/.htpop.sqlite'); chmod(__DIR__ . '/../../data/.htpop.sqlite', 0777); } $dbName = __DIR__ . '/../../data/.htpop.sqlite'; $realDbName = "__DIR__ . '/../../data/.htpop.sqlite'"; $dbPrefix = $console->prompt('DB Table Prefix: [pop_] '); $console->write(); } else { $dbCheck = 1; while (null !== $dbCheck) { $dbName = $console->prompt('DB Name: '); $dbUser = $console->prompt('DB User: '******'DB Password: '******'DB Host: [localhost] '); $dbPrefix = $console->prompt('DB Table Prefix: [pop_] '); if ($dbHost == '') { $dbHost = 'localhost'; } $dbCheck = Db::check($dbInterface, ['database' => $dbName, 'username' => $dbUser, 'password' => $dbPass, 'host' => $dbHost, 'type' => $dbType]); if (null !== $dbCheck) { $console->write(); $console->write($console->colorize('Database configuration test failed. Please try again.', Console::BOLD_RED)); } else { $realDbName = "'" . $dbName . "'"; $console->write(); $console->write($console->colorize('Database configuration test passed.', Console::BOLD_GREEN)); } $console->write(); } } // Install database $sql = stripos($dbInterface, 'pdo') !== false ? __DIR__ . '/../data/pop.' . strtolower($dbType) . '.sql' : __DIR__ . '/../data/pop.' . strtolower($dbInterface) . '.sql'; if ($dbPrefix == '') { $dbPrefix = 'pop_'; } Db::install($sql, ['database' => $dbName, 'username' => $dbUser, 'password' => $dbPass, 'host' => $dbHost, 'prefix' => $dbPrefix, 'type' => $dbType], $dbInterface); // Write config file $config = str_replace(["define('DB_PREFIX', '');", "'adapter' => '',", "'database' => '',", "'username' => '',", "'password' => '',", "'host' => '',", "'type' => null"], ["define('DB_PREFIX', '" . $dbPrefix . "');", "'adapter' => '" . strtolower($dbInterface) . "',", "'database' => " . $realDbName . ",", "'username' => '" . $dbUser . "',", "'password' => '" . $dbPass . "',", "'host' => '" . $dbHost . "',", "'type' => '" . $dbType . "'"], file_get_contents(__DIR__ . '/../../app/config/application.orig.php')); file_put_contents(__DIR__ . '/../../app/config/application.php', $config); $console->write($console->colorize('Application configuration completed.', Console::BOLD_GREEN)); } } $console->write(); $console->write('Thank you for using Pop!'); $console->write(); }