Skip to content

Commit 6db8511

Browse files
committed
Initial commit
Add ServiceProviders and example propel config
1 parent a947e97 commit 6db8511

File tree

7 files changed

+267
-2
lines changed

7 files changed

+267
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
composer.lock

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1-
laravel-propel
1+
propel-laravel
22
==============
33

4-
Propel integration for Laravel framework
4+
Propel2 integration for Laravel framework 4
5+
6+
Usage
7+
-----
8+
9+
Require this package with composer using the following command:
10+
11+
composer require allboo/propel-laravel
12+
13+
After updating composer, add the ServiceProviders to the providers array in app/config/app.php
14+
15+
'Allboo\PropelLaravel\GeneratorServiceProvider',
16+
'Allboo\PropelLaravel\RuntimeServiceProvider',
17+
18+
Create Propel configuration file `app/config/propel.php`
19+
Note: See example config in `example/config/propel.php`
20+
Within provided config schemas files are located into `app/database/` folder, models are generated into `app/models`, migrations into `app/database/migrations`
21+
22+
23+
You can now use Propel commands via artisan, ex
24+
25+
php artisan propel:build
26+
27+
etc.
28+
29+
30+
Services
31+
--------
32+
33+
No service is provided.
34+
35+
Propel configures and manages itself by using static methods and its own service container, so no service is registered into Application.
36+
Actually, the `GeneratorServiceProvider` class injects Propel tasks into artisan tasks list with prefix `propel:`
37+
`RuntimeServiceProvider` class initializes Propel runtime configuration
38+
39+
40+
See also
41+
--------
42+
[Make Propel models work with Laravel Form::model() without making it an array](https://github.com/stephangroen/propel-laravel)

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "allboo/propel-laravel",
3+
"description": "Propel integration for Laravel framework.",
4+
"keywords": ["laravel", "propel", "orm", "Active Record"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Alex Kazinskiy",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.4.0",
15+
"laravel/framework": "4.2.*",
16+
"propel/propel": "~2.0@dev"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Allboo\\PropelLaravel\\": "src"
21+
}
22+
}
23+
}

example/config/propel.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
return [
4+
'propel' => [
5+
'general' => [
6+
'project' => 'MyProject',
7+
'version' => '1.0',
8+
],
9+
10+
'paths' => [
11+
'projectDir' => app_path() . '/propel',
12+
'schemaDir' => app_path() . '/database',
13+
'outputDir' => app_path() . '/propel',
14+
'phpDir' => app_path() . '/models',
15+
'phpConfDir' => app_path() . '/propel',
16+
'sqlDir' => app_path() . '/database',
17+
'migrationDir' => app_path() . '/database/migrations',
18+
'composerDir' => base_path(),
19+
],
20+
21+
'database' => [
22+
'connections' => array_map(function($item) {
23+
return [
24+
'adapter' => $item['driver'],
25+
'dsn' => $item['driver'] . ':host=' . $item['host'] . ';port=' . (empty($item['port']) ? '3306' : $item['port']) . ';dbname=' . $item['database'],
26+
'user' => $item['username'],
27+
'password' => $item['password'],
28+
'settings' => [
29+
'charset' => $item['charset'],
30+
'queries' => [
31+
'SET NAMES utf8 COLLATE utf8_unicode_ci, COLLATION_CONNECTION = utf8_unicode_ci, COLLATION_DATABASE = utf8_unicode_ci, COLLATION_SERVER = utf8_unicode_ci'
32+
],
33+
],
34+
];
35+
}, app()['config']->get('database.connections')),
36+
'adapters' => [
37+
'mysql' => [
38+
'tableType' => 'InnoDB'
39+
],
40+
],
41+
],
42+
43+
'runtime' => [
44+
'defaultConnection' => app()['config']->get('database.default'),
45+
'connections' => array_keys(app()['config']->get('database.connections')),
46+
],
47+
48+
'generator' => [
49+
'defaultConnection' => app()['config']->get('database.default'),
50+
'connections' => array_keys(app()['config']->get('database.connections')),
51+
52+
'targetPackage' => '',
53+
'namespaceAutoPackage' => false,
54+
],
55+
]
56+
];

provides.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"providers": [
3+
"Allboo\PropelLaravel\GeneratorServiceProvider",
4+
"Allboo\PropelLaravel\RuntimeServiceProvider",
5+
]
6+
}

src/GeneratorServiceProvider.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 Symfony\Component\Finder\Finder;
15+
16+
class GeneratorServiceProvider extends ServiceProvider
17+
{
18+
19+
/**
20+
* Indicates if loading of the provider is deferred.
21+
*
22+
* @var bool
23+
*/
24+
protected $defer = true;
25+
26+
/**
27+
* Register the service provider.
28+
*
29+
* @return void
30+
*/
31+
public function register()
32+
{
33+
if (!class_exists('Propel\\Runtime\\Propel', true)) {
34+
throw new \InvalidArgumentException('Unable to find Propel, did you install it?');
35+
}
36+
37+
$finder = new Finder();
38+
$finder->files()->name('*.php')->in(base_path().'/vendor/propel/propel/src/Propel/Generator/Command')->depth(0);
39+
40+
$commands = [];
41+
foreach ($finder as $file) {
42+
$ns = '\\Propel\\Generator\\Command';
43+
$r = new \ReflectionClass($ns.'\\'.$file->getBasename('.php'));
44+
if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) {
45+
$c = $r->newInstance();
46+
47+
$command = 'command.propel.' . $c->getName();
48+
$commands[] = $command;
49+
50+
$c->setName('propel:' . $c->getName());
51+
$c->setAliases(array_map(function($item) {return 'propel:' . $item;}, $c->getAliases()));
52+
53+
$this->app[$command] = $this->app->share(
54+
function ($app) use ($c) {
55+
chdir(app_path() . '/config');
56+
return $c;
57+
}
58+
);
59+
}
60+
}
61+
62+
$this->commands($commands);
63+
}
64+
65+
/**
66+
* Get the services provided by the provider.
67+
*
68+
* @return array
69+
*/
70+
public function provides()
71+
{
72+
return array('command.propel');
73+
}
74+
75+
}

src/RuntimeServiceProvider.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)