setDb() public static méthode

Set DB connection
public static setDb ( Db $db, boolean $isDefault = false ) : void
$db Db
$isDefault boolean
Résultat void
Exemple #1
0
 /**
  * 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;
 }
Exemple #2
0
 public function bootstrap($autoloader = null)
 {
     parent::bootstrap($autoloader);
     $this->on('app.init', function ($application) {
         Record::setDb($application->services['database']);
     });
 }
Exemple #3
0
 /**
  * Initialize the application
  *
  * @param  Application $application
  * @throws \Phire\Exception
  * @return Module
  */
 public function register(Application $application)
 {
     parent::register($application);
     // Set the database
     if ($this->application->services()->isAvailable('database')) {
         Record::setDb($this->application->getService('database'));
         $db = count($this->application->getService('database')->getTables()) > 0;
     } else {
         $db = false;
     }
     $this->application->mergeConfig(['db' => $db]);
     // Check PHP version
     if (version_compare(PHP_VERSION, '5.4.0') < 0) {
         throw new \Phire\Exception('Error: Phire CMS requires PHP 5.4.0 or greater.');
     }
     // Add route params for the controllers
     if (null !== $this->application->router()) {
         $this->application->router()->addControllerParams('*', ['application' => $this->application, 'console' => new Console(120, '    ')]);
     }
     // Set up triggers to check the application session
     $this->application->on('app.route.post', 'Phire\\Event\\Db::check', 1000);
     $this->application->on('app.route.pre', function () {
         if (isset($_SERVER['argv'][1]) && ($_SERVER['argv'][1] != 'sql' && $_SERVER['argv'][1] != 'archive')) {
             echo PHP_EOL . '    Phire Console' . PHP_EOL;
             echo '    =============' . PHP_EOL . PHP_EOL;
         }
     }, 1000);
     $this->application->on('app.dispatch.post', function () {
         echo PHP_EOL;
     }, 1000);
     return $this;
 }
Exemple #4
0
 /**
  * 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;
 }
Exemple #5
0
 /**
  * Initialize the application
  *
  * @param  Application $application
  * @throws Exception
  * @return Module
  */
 public function register(Application $application)
 {
     parent::register($application);
     // Set the database
     if ($this->application->services()->isAvailable('database')) {
         Record::setDb($this->application->getService('database'));
         $db = count($this->application->getService('database')->getTables()) > 0;
     } else {
         $db = false;
     }
     $this->application->mergeConfig(['db' => $db]);
     // Load assets, if they haven't been loaded already
     $this->loadAssets(__DIR__ . '/../data/assets', 'phire');
     if ($db) {
         $systemTheme = Table\Config::findById('system_theme')->value;
         if (file_exists(CONTENT_ABS_PATH . '/phire/themes/' . $systemTheme)) {
             $this->loadAssets(CONTENT_ABS_PATH . '/phire/themes/' . $systemTheme, $systemTheme);
         } else {
             if (file_exists(__DIR__ . '/../data/themes/' . $systemTheme)) {
                 $this->loadAssets(__DIR__ . '/../data/themes/' . $systemTheme, $systemTheme);
             }
         }
     } else {
         $this->loadAssets(__DIR__ . '/../data/themes/default', 'default');
     }
     sort($this->assets['js']);
     sort($this->assets['css']['link']);
     sort($this->assets['css']['import']);
     // Load any custom/override assets
     $this->loadAssets(CONTENT_ABS_PATH . '/phire/assets', 'phire-custom', true);
     // Check PHP version
     if (version_compare(PHP_VERSION, '5.4.0') < 0) {
         throw new Exception('Error: Phire CMS requires PHP 5.4.0 or greater.');
     }
     // Add route params for the controllers
     if (null !== $this->application->router()) {
         $this->application->router()->addControllerParams('*', ['application' => $this->application, 'request' => new Request(), 'response' => new Response()]);
     }
     // Set up triggers to check the application session
     $this->application->on('app.route.pre', 'Phire\\Event\\Ssl::check', 1000)->on('app.route.post', 'Phire\\Event\\Db::check', 1000)->on('app.dispatch.pre', 'Phire\\Event\\Session::check', 1001)->on('app.dispatch.pre', 'Phire\\Event\\Acl::check', 1000);
     // Add roles to user nav
     $this->addRoles();
     // Register modules
     $this->registerModules();
     return $this;
 }
 public function bootstrap($autoloader = null)
 {
     parent::bootstrap($autoloader);
     $this->on('app.init', function ($application) {
         Record::setDb($application->services['database']);
     });
     if ($this->router->isCli()) {
         $this->on('app.route.pre', function () {
             echo PHP_EOL;
             echo '    Pop Tutorial CLI' . PHP_EOL;
             echo '    ----------------' . PHP_EOL . PHP_EOL;
         });
         $this->on('app.dispatch.post', function () {
             echo PHP_EOL;
             echo '    ----------------' . PHP_EOL;
             echo '    Complete!' . PHP_EOL . PHP_EOL;
         });
     }
 }
<?php

/*
*  2015 Lace Cart
*
*  @author LaceCart Dev <*****@*****.**>
*  @copyright  2015 LaceCart Team
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of LaceCart Team
*/
use Pop\Db\Db as DB;
use Pop\Db\Record as Adapter;
return ['services' => ['session' => ['call' => 'Pop\\Web\\Session::getInstance'], 'db' => ['call' => function () use($config) {
    Adapter::setDb(DB::connect($config->database->adapter, ['database' => $config->database->database, 'username' => $config->database->username, 'password' => $config->database->password, 'host' => $config->database->host]));
}], 'config' => ['call' => function () use($config) {
    return $config;
}], 'nav' => ['call' => function () use($nav) {
    return $nav;
}]]];
 /**
  * Constructor
  *
  * Instantiate a project object
  *
  * @param  mixed           $config
  * @param  array           $module
  * @param  \Pop\Mvc\Router $router
  * @return \Pop\Project\Project
  */
 public function __construct($config = null, array $module = null, Router $router = null)
 {
     if (null !== $config) {
         $this->loadConfig($config);
     }
     if (null !== $module) {
         $this->loadModule($module);
     }
     if (null !== $router) {
         $this->loadRouter($router);
     }
     $this->events = new \Pop\Event\Manager();
     $this->services = new \Pop\Service\Locator();
     if (isset($this->config->log)) {
         if (!file_exists($this->config->log)) {
             touch($this->config->log);
             chmod($this->config->log, 0777);
         }
         $this->logger = new \Pop\Log\Logger(new \Pop\Log\Writer\File(realpath($this->config->log)));
     }
     if (isset($this->config->defaultDb)) {
         $default = $this->config->defaultDb;
         \Pop\Db\Record::setDb($this->config->databases->{$default});
     }
 }
Exemple #9
0
 public function testGetDefaultDb()
 {
     Record::setDb(Db::factory('Sqlite', array('database' => __DIR__ . '/../tmp/test.sqlite')), true);
     $this->assertInstanceOf('Pop\\Db\\Db', Record::getDb());
 }