|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Laravel Propel integration |
| 4 | + * |
| 5 | + * @author Alex Kazinskiy <[email protected]> |
| 6 | + * @copyright 2014 Alex Kazinskiy |
| 7 | + * @license http://www.opensource.org/licenses/mit-license.php MIT |
| 8 | + * @link https://github.com/alboo/laravel-propel |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Allboo\PropelLaravel; |
| 12 | + |
| 13 | +use Illuminate\Support\ServiceProvider; |
| 14 | +use Propel\Runtime\Propel; |
| 15 | + |
| 16 | +class RuntimeServiceProvider extends ServiceProvider |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * Bootstrap the application events. |
| 21 | + * |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function boot() |
| 25 | + { |
| 26 | + if (!$this->app->config['propel.propel.runtime.connections']) { |
| 27 | + throw new \InvalidArgumentException('Unable to guess Propel runtime config file. Please, initialize the "propel.runtime" parameter.'); |
| 28 | + } |
| 29 | + |
| 30 | + /** @var \Propel\Runtime\ServiceContainer\StandardServiceContainer */ |
| 31 | + $serviceContainer = \Propel\Runtime\Propel::getServiceContainer(); |
| 32 | + $serviceContainer->closeConnections(); |
| 33 | + $serviceContainer->checkVersion('2.0.0-dev'); |
| 34 | + |
| 35 | + $propel_conf = $this->app->config['propel.propel']; |
| 36 | + foreach ($propel_conf['runtime']['connections'] as $connection_name) { |
| 37 | + $config = $propel_conf['database']['connections'][$connection_name]; |
| 38 | + if (!isset($config['classname'])) { |
| 39 | + $config['classname'] = '\\Propel\\Runtime\\Connection\\ConnectionWrapper'; |
| 40 | + } |
| 41 | + |
| 42 | + $serviceContainer->setAdapterClass($connection_name, $config['adapter']); |
| 43 | + $manager = new \Propel\Runtime\Connection\ConnectionManagerSingle(); |
| 44 | + $manager->setConfiguration($config); |
| 45 | + $manager->setName($connection_name); |
| 46 | + $serviceContainer->setConnectionManager($connection_name, $manager); |
| 47 | + } |
| 48 | + |
| 49 | + $serviceContainer->setDefaultDatasource($propel_conf['runtime']['defaultConnection']); |
| 50 | + |
| 51 | + Propel::setServiceContainer($serviceContainer); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Register the service provider. |
| 56 | + * |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + public function register() |
| 60 | + { |
| 61 | + if (!class_exists('Propel\\Runtime\\Propel', true)) { |
| 62 | + throw new \InvalidArgumentException('Unable to find Propel, did you install it?'); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments