/** * Register the Illuminate Database service * * @param Silex\Application */ public function register(Application $app) { $app['db.connection_defaults'] = array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'test', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => null); $app['db.boot'] = true; $app['db.global'] = true; $app['db.container'] = $app->share(function () { return new Container(); }); $app['db.dispatcher'] = $app->share(function () use($app) { return new Dispatcher($app['db.container']); }); if (class_exists('\\Illuminate\\Cache\\CacheManager')) { $app['db.cache_manager'] = $app->share(function () use($app) { return new \Illuminate\Cache\CacheManager($app['db.container']); }); } $app['db.cache'] = array('driver' => 'apc', 'prefix' => 'laravel'); $app['db'] = $app->share(function () use($app) { $db = new Capsule($app['db.container']); $db->setEventDispatcher($app['db.dispatcher']); if (isset($app['db.cache_manager']) && isset($app['db.cache'])) { $db->setCacheManager($app['db.cache_manager']); foreach ($app['db.cache'] as $key => $value) { $app['db.container']->offsetGet('config')->offsetSet('cache.' . $key, $value); } } if ($app['db.global']) { $db->setAsGlobal(); } if ($app['db.boot']) { $db->bootEloquent(); } if (!isset($app['db.connections'])) { $connection = array(); if (isset($app['db.connection'])) { $connection = $app['db.connection']; } $app['db.connections'] = array('default' => $connection); } foreach ($app['db.connections'] as $connection => $options) { $db->addConnection(array_replace($app['db.connection_defaults'], $options), $connection); } if (!$app['debug']) { $db->connection()->disableQueryLog(); } return $db; }); }
/** * Register the Capsule service. * * @param Application $app **/ public function register(Application $app) { $app['capsule.connection_defaults'] = array('driver' => 'mysql', 'host' => 'localhost', 'database' => null, 'username' => 'root', 'password' => null, 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => null, 'logging' => false); $app['capsule.global'] = true; $app['capsule.eloquent'] = true; $app['capsule.container'] = $app->share(function () { return new Container(); }); $app['capsule.dispatcher'] = $app->share(function () use($app) { return new Dispatcher($app['capsule.container']); }); if (class_exists('Illuminate\\Cache\\CacheManager')) { $app['capsule.cache_manager'] = $app->share(function () use($app) { return new CacheManager($app['capsule.container']); }); } $app['capsule'] = $app->share(function ($name) use($app) { $capsule = new Capsule($app['capsule.container']); $capsule->setEventDispatcher($app['capsule.dispatcher']); if (isset($app['capsule.cache_manager']) && isset($app['capsule.cache'])) { $capsule->setCacheManager($app['capsule.cache_manager']); foreach ($app['capsule.cache'] as $key => $value) { $app['capsule.container']->offsetGet('config')->offsetSet('cache.' . $key, $value); } } if ($app['capsule.global']) { $capsule->setAsGlobal(); } if ($app['capsule.eloquent']) { $capsule->bootEloquent(); } if (!isset($app['capsule.connections'])) { $app['capsule.connections'] = array('default' => isset($app['capsule.connection']) ? $app['capsule.connection'] : array()); } foreach ($app['capsule.connections'] as $connection => $options) { $options = array_replace($app['capsule.connection_defaults'], $options); $logging = $options['logging']; unset($options['logging']); $capsule->addConnection($options, $connection); if ($logging) { $capsule->connection($connection)->enableQueryLog(); } else { $capsule->connection($connection)->disableQueryLog(); } } return $capsule; }); }
/** * Set the cache manager to be used by connections. * * @param \Illuminate\Cache\CacheManager $cache * @return void */ public function setCacheManager(CacheManager $cache) { $this->capsule->setCacheManager($cache); }
<?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']);
$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(); var_dump($user); }); $app->run();