public function __construct(array $connections) { $useEloquent = true; $fetchMode = null; $defaultConnName = null; $capsule = new Capsule(); foreach ($connections as $name => $conn) { if (!$useEloquent && isset($conn['eloquent'])) { $useEloquent = true; } if ($fetchMode === null && isset($conn['fetch_mode']) && !empty($conn['fetch_mode'])) { $fetchMode = $conn['fetch_mode']; } if ($defaultConnName === null && isset($conn['default']) && $conn['default'] === true) { $defaultConnName = $name; } $capsule->addConnection($this->normaliseConfigKeys($conn), $name); } // Set the Capsule configuration options $config = $capsule->getContainer()->make('config'); $config['database.fetch'] = $fetchMode ?: PDO::FETCH_ASSOC; $config['database.default'] = $defaultConnName ?: 'default'; $capsule->getContainer()->instance('config', $config); // If the users are using eloquent, lets boot it if ($useEloquent) { $capsule->bootEloquent(); } $capsule->setAsGlobal(); $this->capsule = $capsule; }
/** * Get the IoC container instance. * * @return \Illuminate\Container\Container */ public function getContainer() { return $this->capsule->getContainer(); }
<?php namespace App; include dirname(__FILE__) . "/../config/config.php"; use Illuminate\Container\Container; // Only needed for DB use Illuminate\Events\Dispatcher; use Illuminate\Filesystem\Filesystem; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Cache\CacheManager; $dbc = new DB(); $dbc->addConnection(['driver' => 'mysql', 'host' => $config['host'], 'database' => $config['database'], 'username' => $config['username'], 'password' => $config['password'], 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false]); // $dbc->setFetchMode(PDO::FETCH_CLASS); $container = $dbc->getContainer(); $container['config']['cache.driver'] = $config['cache_driver']; $container['config']['cache.path'] = $config['cache_path']; $container['config']['cache.prefix'] = "rr20"; $container['files'] = new Filesystem(); $container['config']['cache.memcached'] = ['host' => $config['cache_servers'], 'port' => $config['cache_port'], 'weight' => 100]; $container->offsetGet('config')->offsetSet('cache.driver', 'array'); $cacheManager = new CacheManager($container); $dbc->setEventDispatcher(new Dispatcher(new Container())); $dbc->setCacheManager($cacheManager); $dbc->setAsGlobal(); $dbc->bootEloquent(); global $dbc; // $cache = new \Blablacar\Memcached\Client(); // $cache->addServer($config['cache_servers'], $config['cache_port']);
$app->get('/', function () use($cache_dir) { // Filesystem cache $capsule = new Capsule(); $container = $capsule->getContainer(); $container['config']['cache.driver'] = 'file'; $container['config']['cache.path'] = $cache_dir; $container['files'] = new Filesystem(); $capsule->setCacheManager(new CacheManager($container)); $cache = $container->make('cache'); $cache->put('cache-test', 'Howdy. I am teh cache.', 500); echo $cache->get('cache-test'); }); $app->get('/cacheDatabase', function () use($cache_dir) { // Filesystem cache, merged with basic database connection for 'remember' $capsule = new Capsule(); $container = $capsule->getContainer(); $container['config']['cache.driver'] = 'file'; $container['config']['cache.path'] = $cache_dir; $container['files'] = new Filesystem(); $capsule->addConnection(['driver' => 'mysql', 'host' => 'localhost', 'database' => 'illuminate_non_laravel', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '']); // Set the event dispatcher used by Eloquent models... (optional) $capsule->setEventDispatcher(new Dispatcher(new Container())); // Set the cache manager instance used by connections... (optional) $capsule->setCacheManager(new CacheManager($container)); // Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent(); // Use it echo '<pre>'; $user = Capsule::table('users')->where('id', 1)->remember(10)->get();