From 6db85116903a34e5e5ca19225c3d8db89c97192b Mon Sep 17 00:00:00 2001 From: Alex Kazinskiy Date: Tue, 4 Nov 2014 18:42:05 +0400 Subject: [PATCH] Initial commit Add ServiceProviders and example propel config --- .gitignore | 2 + README.md | 42 +++++++++++++++++- composer.json | 23 ++++++++++ example/config/propel.php | 56 ++++++++++++++++++++++++ provides.json | 6 +++ src/GeneratorServiceProvider.php | 75 ++++++++++++++++++++++++++++++++ src/RuntimeServiceProvider.php | 65 +++++++++++++++++++++++++++ 7 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 example/config/propel.php create mode 100644 provides.json create mode 100644 src/GeneratorServiceProvider.php create mode 100644 src/RuntimeServiceProvider.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8b7ef35 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor +composer.lock diff --git a/README.md b/README.md index bf1890f..2b9648a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,42 @@ -laravel-propel +propel-laravel ============== -Propel integration for Laravel framework +Propel2 integration for Laravel framework 4 + +Usage +----- + +Require this package with composer using the following command: + + composer require allboo/propel-laravel + +After updating composer, add the ServiceProviders to the providers array in app/config/app.php + + 'Allboo\PropelLaravel\GeneratorServiceProvider', + 'Allboo\PropelLaravel\RuntimeServiceProvider', + +Create Propel configuration file `app/config/propel.php` +Note: See example config in `example/config/propel.php` +Within provided config schemas files are located into `app/database/` folder, models are generated into `app/models`, migrations into `app/database/migrations` + + +You can now use Propel commands via artisan, ex + + php artisan propel:build + +etc. + + +Services +-------- + +No service is provided. + +Propel configures and manages itself by using static methods and its own service container, so no service is registered into Application. +Actually, the `GeneratorServiceProvider` class injects Propel tasks into artisan tasks list with prefix `propel:` +`RuntimeServiceProvider` class initializes Propel runtime configuration + + +See also +-------- +[Make Propel models work with Laravel Form::model() without making it an array](https://github.com/stephangroen/propel-laravel) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..73a6432 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "allboo/propel-laravel", + "description": "Propel integration for Laravel framework.", + "keywords": ["laravel", "propel", "orm", "Active Record"], + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Alex Kazinskiy", + "email": "alboo@list.ru" + } + ], + "require": { + "php": ">=5.4.0", + "laravel/framework": "4.2.*", + "propel/propel": "~2.0@dev" + }, + "autoload": { + "psr-4": { + "Allboo\\PropelLaravel\\": "src" + } + } +} diff --git a/example/config/propel.php b/example/config/propel.php new file mode 100644 index 0000000..2663123 --- /dev/null +++ b/example/config/propel.php @@ -0,0 +1,56 @@ + [ + 'general' => [ + 'project' => 'MyProject', + 'version' => '1.0', + ], + + 'paths' => [ + 'projectDir' => app_path() . '/propel', + 'schemaDir' => app_path() . '/database', + 'outputDir' => app_path() . '/propel', + 'phpDir' => app_path() . '/models', + 'phpConfDir' => app_path() . '/propel', + 'sqlDir' => app_path() . '/database', + 'migrationDir' => app_path() . '/database/migrations', + 'composerDir' => base_path(), + ], + + 'database' => [ + 'connections' => array_map(function($item) { + return [ + 'adapter' => $item['driver'], + 'dsn' => $item['driver'] . ':host=' . $item['host'] . ';port=' . (empty($item['port']) ? '3306' : $item['port']) . ';dbname=' . $item['database'], + 'user' => $item['username'], + 'password' => $item['password'], + 'settings' => [ + 'charset' => $item['charset'], + 'queries' => [ + 'SET NAMES utf8 COLLATE utf8_unicode_ci, COLLATION_CONNECTION = utf8_unicode_ci, COLLATION_DATABASE = utf8_unicode_ci, COLLATION_SERVER = utf8_unicode_ci' + ], + ], + ]; + }, app()['config']->get('database.connections')), + 'adapters' => [ + 'mysql' => [ + 'tableType' => 'InnoDB' + ], + ], + ], + + 'runtime' => [ + 'defaultConnection' => app()['config']->get('database.default'), + 'connections' => array_keys(app()['config']->get('database.connections')), + ], + + 'generator' => [ + 'defaultConnection' => app()['config']->get('database.default'), + 'connections' => array_keys(app()['config']->get('database.connections')), + + 'targetPackage' => '', + 'namespaceAutoPackage' => false, + ], + ] +]; diff --git a/provides.json b/provides.json new file mode 100644 index 0000000..fc79b8b --- /dev/null +++ b/provides.json @@ -0,0 +1,6 @@ +{ + "providers": [ + "Allboo\PropelLaravel\GeneratorServiceProvider", + "Allboo\PropelLaravel\RuntimeServiceProvider", + ] +} \ No newline at end of file diff --git a/src/GeneratorServiceProvider.php b/src/GeneratorServiceProvider.php new file mode 100644 index 0000000..c46f548 --- /dev/null +++ b/src/GeneratorServiceProvider.php @@ -0,0 +1,75 @@ + + * @copyright 2014 Alex Kazinskiy + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link https://github.com/alboo/laravel-propel + */ + +namespace Allboo\PropelLaravel; + +use Illuminate\Support\ServiceProvider; +use Symfony\Component\Finder\Finder; + +class GeneratorServiceProvider extends ServiceProvider +{ + + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = true; + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + if (!class_exists('Propel\\Runtime\\Propel', true)) { + throw new \InvalidArgumentException('Unable to find Propel, did you install it?'); + } + + $finder = new Finder(); + $finder->files()->name('*.php')->in(base_path().'/vendor/propel/propel/src/Propel/Generator/Command')->depth(0); + + $commands = []; + foreach ($finder as $file) { + $ns = '\\Propel\\Generator\\Command'; + $r = new \ReflectionClass($ns.'\\'.$file->getBasename('.php')); + if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) { + $c = $r->newInstance(); + + $command = 'command.propel.' . $c->getName(); + $commands[] = $command; + + $c->setName('propel:' . $c->getName()); + $c->setAliases(array_map(function($item) {return 'propel:' . $item;}, $c->getAliases())); + + $this->app[$command] = $this->app->share( + function ($app) use ($c) { + chdir(app_path() . '/config'); + return $c; + } + ); + } + } + + $this->commands($commands); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('command.propel'); + } + +} diff --git a/src/RuntimeServiceProvider.php b/src/RuntimeServiceProvider.php new file mode 100644 index 0000000..f5fd61a --- /dev/null +++ b/src/RuntimeServiceProvider.php @@ -0,0 +1,65 @@ + + * @copyright 2014 Alex Kazinskiy + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link https://github.com/alboo/laravel-propel + */ + +namespace Allboo\PropelLaravel; + +use Illuminate\Support\ServiceProvider; +use Propel\Runtime\Propel; + +class RuntimeServiceProvider extends ServiceProvider +{ + + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + if (!$this->app->config['propel.propel.runtime.connections']) { + throw new \InvalidArgumentException('Unable to guess Propel runtime config file. Please, initialize the "propel.runtime" parameter.'); + } + + /** @var \Propel\Runtime\ServiceContainer\StandardServiceContainer */ + $serviceContainer = \Propel\Runtime\Propel::getServiceContainer(); + $serviceContainer->closeConnections(); + $serviceContainer->checkVersion('2.0.0-dev'); + + $propel_conf = $this->app->config['propel.propel']; + foreach ($propel_conf['runtime']['connections'] as $connection_name) { + $config = $propel_conf['database']['connections'][$connection_name]; + if (!isset($config['classname'])) { + $config['classname'] = '\\Propel\\Runtime\\Connection\\ConnectionWrapper'; + } + + $serviceContainer->setAdapterClass($connection_name, $config['adapter']); + $manager = new \Propel\Runtime\Connection\ConnectionManagerSingle(); + $manager->setConfiguration($config); + $manager->setName($connection_name); + $serviceContainer->setConnectionManager($connection_name, $manager); + } + + $serviceContainer->setDefaultDatasource($propel_conf['runtime']['defaultConnection']); + + Propel::setServiceContainer($serviceContainer); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + if (!class_exists('Propel\\Runtime\\Propel', true)) { + throw new \InvalidArgumentException('Unable to find Propel, did you install it?'); + } + } +}