setConnectionResolver() public static method

Set the connection resolver instance.
public static setConnectionResolver ( Illuminate\Database\ConnectionResolverInterface $resolver ) : void
$resolver Illuminate\Database\ConnectionResolverInterface
return void
Example #1
2
 /**
  * Initialize the Laravel framework.
  * @param SymfonyRequest $request
  */
 private function initialize($request = null)
 {
     // Store a reference to the database object
     // so the database connection can be reused during tests
     $oldDb = null;
     if ($this->app['db'] && $this->app['db']->connection()) {
         $oldDb = $this->app['db'];
     }
     // The module can login a user with the $I->amLoggedAs() method,
     // but this is not persisted between requests. Store a reference
     // to the logged in user to simulate this.
     $loggedInUser = null;
     if ($this->app['auth'] && $this->app['auth']->check()) {
         $loggedInUser = $this->app['auth']->user();
     }
     // Load the application object
     $this->app = $this->kernel = $this->loadApplication();
     // Set the request instance for the application
     if (is_null($request)) {
         $appConfig = (require $this->module->config['project_dir'] . 'config/app.php');
         $request = SymfonyRequest::create($appConfig['url']);
     }
     $this->app->instance('request', Request::createFromBase($request));
     $this->app->instance('middleware.disable', $this->module->config['disable_middleware']);
     // Bootstrap the application
     $this->app->make('Illuminate\\Contracts\\Http\\Kernel')->bootstrap();
     // Restore the old database object if available
     if ($oldDb) {
         $this->app['db'] = $oldDb;
         Model::setConnectionResolver($this->app['db']);
     }
     // If there was a user logged in restore this user.
     // Also reload the user object from the user provider to prevent stale user data.
     if ($loggedInUser) {
         $refreshed = $this->app['auth']->getProvider()->retrieveById($loggedInUser->getAuthIdentifier());
         $this->app['auth']->setUser($refreshed ?: $loggedInUser);
     }
     $this->module->setApplication($this->app);
 }
Example #2
1
 /**
  * Initialize the Laravel framework.
  */
 private function initialize()
 {
     // Store a reference to the database object
     // so the database connection can be reused during tests
     $oldDb = null;
     if ($this->app['db'] && $this->app['db']->connection()) {
         $oldDb = $this->app['db'];
     }
     // The module can login a user with the $I->amLoggedAs() method,
     // but this is not persisted between requests. Store a reference
     // to the logged in user to simulate this.
     $loggedInUser = null;
     if ($this->app['auth'] && $this->app['auth']->check()) {
         $loggedInUser = $this->app['auth']->user();
     }
     $this->app = $this->kernel = $this->loadApplication();
     $this->app->make('Illuminate\\Contracts\\Http\\Kernel')->bootstrap();
     // Set the base url for the Request object
     $url = $this->app['config']->get('app.url', 'http://localhost');
     $this->app->instance('request', Request::createFromBase(SymfonyRequest::create($url)));
     if ($oldDb) {
         $this->app['db'] = $oldDb;
         Model::setConnectionResolver($this->app['db']);
     }
     // If there was a user logged in restore this user.
     // Also reload the user object from the user provider to prevent stale user data.
     if ($loggedInUser) {
         $refreshed = $this->app['auth']->getProvider()->retrieveById($loggedInUser->getAuthIdentifier());
         $this->app['auth']->setUser($refreshed ?: $loggedInUser);
     }
     $this->module->setApplication($this->app);
 }
Example #3
1
 private function setDatabase($dbConfig)
 {
     $capsule = new Capsule();
     $capsule->addConnection($dbConfig, $this->connName);
     $this->set('db', $capsule->getConnection($this->connName));
     //set model conn for all models
     $resolver = new \Illuminate\Database\ConnectionResolver();
     $resolver->addConnection($this->connName, $capsule->getConnection($this->connName));
     $resolver->setDefaultConnection($this->connName);
     \Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
 }
 protected function __construct()
 {
     $this->app = new Container();
     // Register mock instances with IoC container
     $config = $this->getConfig();
     $this->app->singleton('config', function () use($config) {
         return $config;
     });
     list($connector, $manager) = $this->getDatabase();
     $this->app->singleton('db.factory', function () use($connector) {
         return $connector;
     });
     $this->app->singleton('db', function () use($manager) {
         return $manager;
     });
     $auth = $this->getAuth();
     $this->app->singleton('auth', function () use($auth) {
         return $auth;
     });
     $dispatcher = new Dispatcher($this->app);
     $this->app->singleton('events', function () use($dispatcher) {
         return $dispatcher;
     });
     Model::setConnectionResolver($this->app['db']);
     Model::setEventDispatcher($this->app['events']);
 }
Example #5
0
 /**
  * Initialize the Laravel Framework.
  *
  * @throws ModuleConfig
  */
 private function initialize()
 {
     // Store a reference to the database object
     // so the database connection can be reused during tests
     $oldDb = null;
     if ($this->app['db'] && $this->app['db']->connection()) {
         $oldDb = $this->app['db'];
     }
     // Store the current value for the router filters
     // so it can be reset after reloading the application
     $oldFiltersEnabled = null;
     if ($router = $this->app['router']) {
         $property = new \ReflectionProperty(get_class($router), 'filtering');
         $property->setAccessible(true);
         $oldFiltersEnabled = $property->getValue($router);
     }
     $this->app = $this->loadApplication();
     $this->kernel = $this->getStackedClient();
     $this->app->boot();
     // Reset the booted flag of the Application object
     // so the app will be booted again if it receives a new Request
     $property = new \ReflectionProperty(get_class($this->app), 'booted');
     $property->setAccessible(true);
     $property->setValue($this->app, false);
     if ($oldDb) {
         $this->app['db'] = $oldDb;
         Model::setConnectionResolver($this->app['db']);
     }
     if (!is_null($oldFiltersEnabled)) {
         $oldFiltersEnabled ? $this->app['router']->enableFilters() : $this->app['router']->disableFilters();
     }
     $this->module->setApplication($this->app);
 }
Example #6
0
 /**
  * Creates a new instance of Sentry.
  *
  * @return \Cartalyst\Sentry\Sentry
  * @throws \RuntimeException
  */
 public static function createSentry()
 {
     // Get some resources
     $ci =& get_instance();
     $ci->load->driver('session');
     // If Eloquent doesn't exist, then we must assume they are using their own providers.
     if (class_exists('Illuminate\\Database\\Eloquent\\Model')) {
         $ci->load->database();
         // Let's connect and get the PDO instance
         $method = $ci->db->pconnect ? 'db_pconnect' : 'db_connect';
         $pdo = $ci->db->{$method}();
         // Validate PDO
         if (!$pdo instanceof PDO) {
             throw new \RuntimeException("Sentry will only work with PDO database connections.");
         }
         // Setup PDO
         foreach (static::$pdoOptions as $key => $value) {
             $pdo->setAttribute($key, $value);
         }
         $driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
         $tablePrefix = substr($ci->db->dbprefix('.'), 0, -1);
         Eloquent::setConnectionResolver(new ConnectionResolver($pdo, $driverName, $tablePrefix));
     }
     return new BaseSentry($userProvider = new UserProvider(new NativeHasher()), new GroupProvider(), new ThrottleProvider($userProvider), new CISession($ci->session), new CICookie($ci->input), $ci->input->ip_address());
 }
 /**
  * @param $resolver
  */
 protected function bootModels($resolver)
 {
     Model::clearBootedModels();
     Model::setEventDispatcher($this->app['illuminate.events']);
     Model::setConnectionResolver($resolver);
     $this->app['dispatcher']->dispatch(NineEvents::MODELS_BOOTED, new EloquentEvent($this->container->get('db')));
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     $this->app->singleton('flarum.db', function () {
         $factory = new ConnectionFactory($this->app);
         $connection = $factory->make($this->app->make('flarum.config')['database']);
         $connection->setEventDispatcher($this->app->make('Illuminate\\Contracts\\Events\\Dispatcher'));
         $connection->setFetchMode(PDO::FETCH_CLASS);
         return $connection;
     });
     $this->app->alias('flarum.db', 'Illuminate\\Database\\ConnectionInterface');
     $this->app->singleton('Illuminate\\Database\\ConnectionResolverInterface', function () {
         $resolver = new ConnectionResolver(['flarum' => $this->app->make('flarum.db')]);
         $resolver->setDefaultConnection('flarum');
         return $resolver;
     });
     $this->app->alias('Illuminate\\Database\\ConnectionResolverInterface', 'db');
     if (Core::isInstalled()) {
         $this->app->booting(function () {
             $resolver = $this->app->make('Illuminate\\Database\\ConnectionResolverInterface');
             Model::setConnectionResolver($resolver);
             Model::setEventDispatcher($this->app->make('events'));
         });
     }
     $this->app->singleton('Flarum\\Migrations\\MigrationRepositoryInterface', function ($app) {
         return new DatabaseMigrationRepository($app['db'], 'migrations');
     });
 }
 /**
  * Set mock connection.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  */
 protected function addMockConnection(Model $model)
 {
     $resolver = m::mock('\\Illuminate\\Database\\ConnectionResolverInterface');
     $model->setConnectionResolver($resolver);
     $resolver->shouldReceive('connection')->andReturn(m::mock('\\Illuminate\\Database\\Connection'));
     $model->getConnection()->shouldReceive('getQueryGrammar')->andReturn(m::mock('\\Illuminate\\Database\\Query\\Grammars\\Grammar'));
     $model->getConnection()->shouldReceive('getPostProcessor')->andReturn(m::mock('\\Illuminate\\Database\\Query\\Processors\\Processor'));
 }
 public function __construct()
 {
     $settings = array('driver' => 'pgsql', 'host' => 'localhost', 'database' => 'yesbike', 'username' => 'postgres', 'password' => 'macedo', 'charset' => 'UTF8', 'prefix' => '');
     $connFactory = new \Illuminate\Database\Connectors\ConnectionFactory(new \Illuminate\Container\Container());
     $conn = $connFactory->make($settings);
     $resolver = new \Illuminate\Database\ConnectionResolver();
     $resolver->addConnection('default', $conn);
     $resolver->setDefaultConnection('default');
     \Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
 }
Example #11
0
 /**
  * Sets up the Eloquent Connection Resolver with the given PDO connection.
  *
  * @param  PDO    $pdo
  * @param  string $driverName
  * @param  string $tablePrefix
  * @return void
  */
 public static function setupDatabaseResolver(PDO $pdo, $driverName = null, $tablePrefix = '')
 {
     // If Eloquent doesn't exist, then we must assume they are using their own providers.
     if (class_exists('Illuminate\\Database\\Eloquent\\Model')) {
         if (is_null($driverName)) {
             $driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
         }
         Eloquent::setConnectionResolver(new ConnectionResolver($pdo, $driverName, $tablePrefix));
     }
 }
Example #12
0
 private function setConnectionResolver()
 {
     if ($this->checkTablesExists() && is_null(Model::getConnectionResolver())) {
         $res = new ConnectionResolver();
         DB::pretend(function ($db) use($res) {
             $res->addConnection(config('database.default'), $db);
             $res->setDefaultConnection(config('database.default'));
         });
         Model::setConnectionResolver($res);
     }
 }
Example #13
0
 public function call()
 {
     $container = new Illuminate\Container\Container();
     $connFactory = new \Illuminate\Database\Connectors\ConnectionFactory($container);
     $conn = $connFactory->make(array('driver' => 'mysql', 'host' => '127.0.0.1', 'database' => 'flysmuthe', 'username' => \config\SecureConfig::$dbUsername, 'password' => \config\SecureConfig::$dbPassword, 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => ''));
     $resolver = new \Illuminate\Database\ConnectionResolver();
     $resolver->addConnection('default', $conn);
     $resolver->setDefaultConnection('default');
     \Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
     //Call next middleware
     $this->next->call();
 }
Example #14
0
 public function __construct()
 {
     $this->ci =& get_instance();
     $config = $this->ci->db;
     // Get the DB object
     $pdo = new PDO('mysql:host=' . $config->hostname . ';dbname=' . $config->database, $config->username, $config->password);
     $drivers = array('mysql' => '\\Illuminate\\Database\\MySqlConnection', 'pgsql' => '\\Illuminate\\Database\\PostgresConnection', 'sqlite' => '\\Illuminate\\Database\\SQLiteConnection');
     $conn = new $drivers['mysql']($pdo, $config->database, $config->dbprefix);
     $resolver = new Illuminate\Database\ConnectionResolver();
     $resolver->addConnection('default', $conn);
     $resolver->setDefaultConnection('default');
     \Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
 }
Example #15
0
 public function load()
 {
     // Database information : should be inject with DI
     $settings = array('driver' => 'sqlite', 'database' => 'vdm_posts_db.sqlite', 'prefix' => '');
     // Bootstrap Eloquent ORM
     $container = new Container();
     $connFactory = new \Illuminate\Database\Connectors\ConnectionFactory($container);
     $conn = $connFactory->make($settings);
     $resolver = new \Illuminate\Database\ConnectionResolver();
     $resolver->addConnection('default', $conn);
     $resolver->setDefaultConnection('default');
     \Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
 }
Example #16
0
 /**
  * Creates a new Connection via the Factory then adds it to the resolver.  If
  * this is the first time it is ran, it will also initialize the Eloquent
  * Model with the ConnectionResolver object so your models work.
  *
  * @param   string  Name of the connection
  * @param   array   The config array for the connection
  * @param   bool    Whether to make this the default connection
  * @return  Illuminate\Database\Connectors\Connection
  */
 public static function make($name, array $config, $default = false)
 {
     self::setupResolverAndFactory();
     $conn = self::$factory->make($config);
     self::$resolver->addConnection($name, $conn);
     if ($default) {
         self::$resolver->setDefaultConnection($name);
     }
     if (!self::$modelInitialized) {
         Model::setConnectionResolver(self::$resolver);
         self::$modelInitialized = true;
     }
     return $conn;
 }
 static function startUp(KernelInterface $kernel, ModuleInfo $moduleInfo)
 {
     $kernel->onRegisterServices(function (InjectorInterface $injector) {
         $injector->share(DatabaseAPI::class)->prepare(DatabaseAPI::class, function (DatabaseAPI $db) {
             $db->manager->setAsGlobal();
             $db->manager->setEventDispatcher(new Dispatcher($db->manager->getContainer()));
             $db->manager->bootEloquent();
             Model::setConnectionResolver($db);
         })->alias(ModelControllerInterface::class, ModelController::class)->share(ModelController::class)->share(MigrationsSettings::class)->alias(MigrationsInterface::class, Migrations::class);
     });
     if ($kernel->getProfile() instanceof ConsoleProfile) {
         $kernel->onConfigure(function (ConsoleSettings $consoleSettings) {
             $consoleSettings->registerTasksFromClass(MigrationCommands::class);
         });
     }
 }
Example #18
0
 /**
  * Creates a new instance of Sentry.
  *
  * @return Cartalyst\Sentry\Sentry
  */
 public static function createSentry()
 {
     // If Eloquent doesn't exist, then we must assume they are using their own providers.
     if (class_exists('Illuminate\\Database\\Eloquent\\Model')) {
         // Retrieve what we need for our resolver
         $database = Database_Connection::instance();
         $pdo = $database->connection();
         $driverName = $database->driver_name();
         $tablePrefix = $database->table_prefix();
         // Make sure we're getting a PDO connection
         if (!$pdo instanceof PDO) {
             throw new \RuntimeException("Sentry will only work with PDO database connections.");
         }
         Eloquent::setConnectionResolver(new ConnectionResolver($pdo, $driverName, $tablePrefix));
     }
     return new BaseSentry($userProvider = new UserProvider(new NativeHasher()), new GroupProvider(), new ThrottleProvider($userProvider), new FuelPHPSession(Session::instance()), new FuelPHPCookie(), Input::real_ip());
 }
 /**
  * Bootstrap the application events.
  *
  * @return void
  */
 public function boot()
 {
     Model::setConnectionResolver($this->app['db']);
     Model::setEventDispatcher($this->app['events']);
 }
Example #20
0
    // To help the built-in PHP dev server, check if the request was actually for
    // something which should probably be served as a static file
    $file = __DIR__ . $_SERVER['REQUEST_URI'];
    if (is_file($file)) {
        return false;
    }
}
date_default_timezone_set('Europe/Istanbul');
require __DIR__ . '/../vendor/autoload.php';
// Database information
$settings = array('driver' => 'mysql', 'host' => 'localhost', 'port' => 3306, 'database' => 'wtf_db', 'username' => 'root', 'password' => '', 'collation' => 'utf8_turkish_ci', 'charset' => 'utf8', 'prefix' => '');
// Bootstrap Eloquent ORM
$container = new Illuminate\Container\Container();
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory($container);
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
session_start();
// Instantiate the app
$settings = (require __DIR__ . '/../src/settings.php');
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
Example #21
0
 public function testDateTimeAttributesReturnNullIfSetToNull()
 {
     $timestamps = ['created_at' => Carbon\Carbon::now(), 'updated_at' => Carbon\Carbon::now()];
     $model = new EloquentDateModelStub();
     Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver = m::mock('Illuminate\\Database\\ConnectionResolverInterface'));
     $resolver->shouldReceive('connection')->andReturn($mockConnection = m::mock('StdClass'));
     $mockConnection->shouldReceive('getQueryGrammar')->andReturn($mockConnection);
     $mockConnection->shouldReceive('getDateFormat')->andReturn('Y-m-d H:i:s');
     $instance = $model->newInstance($timestamps);
     $instance->created_at = null;
     $this->assertNull($instance->created_at);
 }
Example #22
0
 /**
  * Bootstrap Eloquent so it is ready for usage.
  *
  * @return void
  */
 public function bootEloquent()
 {
     Eloquent::setConnectionResolver($this->manager);
     // If we have an event dispatcher instance, we will go ahead and register it
     // with the Eloquent ORM, allowing for model callbacks while creating and
     // updating "model" instances; however, if it not necessary to operate.
     if ($dispatcher = $this->getEventDispatcher()) {
         Eloquent::setEventDispatcher($dispatcher);
     }
 }
 /**
  * Bootstrap Eloquent.
  *
  * @return void
  */
 public static function setUpBeforeClass()
 {
     Eloquent::setConnectionResolver(new DatabaseIntegrationTestConnectionResolver());
     Eloquent::setEventDispatcher(new Illuminate\Events\Dispatcher());
 }
Example #24
0
 /**
  * Bootstrap Eloquent.
  *
  * @return void
  */
 public static function setUpBeforeClass()
 {
     Eloquent::setConnectionResolver(new ConnectionResolver());
 }
 /**
  * Register the database connections with the Eloquent ORM.
  *
  * @return void
  */
 protected function registerEloquent()
 {
     Model::setConnectionResolver($this->app['db']);
 }
 public function boot(Application $app)
 {
     \Illuminate\Database\Eloquent\Model::setConnectionResolver($app['db.resolver']);
 }
 /**
  * Prepare the Eloquent ORM for use.
  *
  * @return void
  */
 public function bootEloquent()
 {
     Eloquent\Model::setConnectionResolver($this->manager);
     Eloquent\Model::setEventDispatcher($this->config['events']);
 }
Example #28
0
 /**
  * Build a fixture instance.
  *
  * @return void
  */
 protected function buildFixture()
 {
     if ($this->fixture) {
         return;
     }
     $this->db = $this->buildDB();
     $str = new Str();
     $this->fixture = Fixture::getInstance();
     $repository = new Eloquent($this->db, $str);
     $this->fixture->setDriver($repository);
     // Bootstrap Eloquent
     $sqliteConnection = new SQLiteConnection($this->db);
     $resolver = new ConnectionResolver(array('sqlite' => $sqliteConnection));
     $resolver->setDefaultConnection('sqlite');
     Model::setConnectionResolver($resolver);
 }
Example #29
-1
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     $this->app->singleton('Illuminate\\Database\\ConnectionInterface', function () {
         $factory = new ConnectionFactory($this->app);
         $connection = $factory->make($this->app['config']->get('fluxbb.database'));
         $connection->setEventDispatcher($this->app->make('Illuminate\\Contracts\\Events\\Dispatcher'));
         $connection->setFetchMode(PDO::FETCH_CLASS);
         return $connection;
     });
     $this->app->singleton('Illuminate\\Database\\ConnectionResolverInterface', function () {
         $resolver = new ConnectionResolver(['fluxbb' => $this->app->make('Illuminate\\Database\\ConnectionInterface')]);
         $resolver->setDefaultConnection('fluxbb');
         return $resolver;
     });
     if (Core::isInstalled()) {
         $this->app->booting(function () {
             $resolver = $this->app->make('Illuminate\\Database\\ConnectionResolverInterface');
             Model::setConnectionResolver($resolver);
         });
     }
 }