├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src └── Propel │ └── Silex │ └── PropelServiceProvider.php └── tests ├── Propel └── Tests │ └── Silex │ ├── PropelFixtures │ ├── FixtEmpty │ │ └── build │ │ │ ├── classes │ │ │ └── classes.txt │ │ │ └── conf │ │ │ └── conf.txt │ └── FixtFull │ │ └── build │ │ ├── classes │ │ └── mynamespace │ │ │ └── mynamespace.txt │ │ └── conf │ │ ├── classmap-myproject-conf.php │ │ └── myproject-conf.php │ └── PropelServiceProviderTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | composer.phar 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | 7 | before_script: 8 | - wget http://getcomposer.org/composer.phar 9 | - php composer.phar install 10 | 11 | notifications: 12 | email: 13 | - william.durand1@gmail.com 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2012 Cristiano Cinotti, William Durand 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PropelServiceProvider 2 | ===================== 3 | 4 | [![Build Status](https://secure.travis-ci.org/propelorm/PropelServiceProvider.png?branch=master)](http://travis-ci.org/propelorm/PropelServiceProvider) 5 | 6 | The *PropelServiceProvider* provides integration with [Propel](http://www.propelorm.org). 7 | 8 | Parameters 9 | ---------- 10 | 11 | * **propel.path** (optional): The path in which Propel.php will be found. Usually, for 12 | PEAR installation, it is `propel` while for Git installation it is 13 | `vendor/propel/runtime/lib`. 14 | Default is `/full/project/path/vendor/propel/propel1/runtime/lib`. 15 | 16 | * **propel.config_file** (optional): The name of Propel configuration file with full path. 17 | Default is `/full/project/path/build/conf/projectname-conf.php` 18 | 19 | * **propel.model_path** (optional): Path to where model classes are located. 20 | Default is `/full/project/path/build/classes` 21 | 22 | 23 | > It's strongly recommanded to use **absolute paths** for previous options. 24 | 25 | 26 | Services 27 | -------- 28 | 29 | No service is provided. 30 | 31 | Propel configures and manages itself by **using** static methods, so no service is registered into Application. 32 | Actually, the PropelServiceProvider class initializes Propel in a more "Silex-ian" way. 33 | 34 | 35 | Registering 36 | ----------- 37 | 38 | Make sure you place a copy of *Propel* in `vendor/propel` or install it through PEAR, or Composer. 39 | 40 | For more informations consult the [Propel documentation](http://www.propelorm.org/documentation/01-installation.html): 41 | 42 | ``` php 43 | register(new Propel\Silex\PropelServiceProvider()); 48 | ``` 49 | 50 | Alternatively, if you 've installed Propel by Git in `vendor/propel` and 51 | you built your model with default Propel generator options: 52 | 53 | ``` php 54 | register(new Propel\Silex\PropelServiceProvider()); 57 | ``` 58 | 59 | 60 | We can consider "default" Propel generator options: 61 | 62 | * Put `build.properties` and `schema.xml` files into the main directory project, 63 | usually where file `index.php` is located. 64 | 65 | * In `build.properties` file, define only `propel.database`, `propel.project` 66 | and `propel.namespace.autopackage` properties. 67 | 68 | 69 | Usage 70 | ----- 71 | 72 | You'll have to build the model by yourself. According to Propel documentation, you'll need three files: 73 | 74 | * `schema.xml` which contains your database schema; 75 | 76 | * `build.properties` more information below; 77 | 78 | * `runtime-conf.xml` which contains the database configuration. 79 | 80 | 81 | Use the `propel-gen` script to create all files (SQL, configuration, Model classes). 82 | 83 | By default, the *PropelServiceProvider* relies on the Silex autoloader you have to configure to load 84 | model classes. Of course, the Silex autoloader needs the model to be built with namespaces, 85 | so be sure to set this property into the `build.properties` file: 86 | 87 | ``` yaml 88 | propel.namespace.autopackage = true 89 | 90 | The recommended configuration for your `build.properties` file is: 91 | 92 | propel.project = 93 | 94 | propel.namespace.autoPackage = true 95 | propel.packageObjectModel = true 96 | 97 | # Enable full use of the DateTime class. 98 | # Setting this to true means that getter methods for date/time/timestamp 99 | # columns will return a DateTime object when the default format is empty. 100 | propel.useDateTimeClass = true 101 | 102 | # Specify a custom DateTime subclass that you wish to have Propel use 103 | # for temporal values. 104 | propel.dateTimeClass = DateTime 105 | 106 | # These are the default formats that will be used when fetching values from 107 | # temporal columns in Propel. You can always specify these when calling the 108 | # methods directly, but for methods like getByName() it is nice to change 109 | # the defaults. 110 | # To have these methods return DateTime objects instead, you should set these 111 | # to empty values 112 | propel.defaultTimeStampFormat = 113 | propel.defaultTimeFormat = 114 | propel.defaultDateFormat = 115 | ``` 116 | 117 | If you plan to build your model without using namespaces, you need to force Propel to use 118 | its internal autoloader. Do this by setting the option `propel.internal_autoload` to `true`. 119 | 120 | For more information, consult the [Propel documentation](http://www.propelorm.org/documentation/). 121 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "propel/propel-service-provider", 3 | "type": "library", 4 | "description": "Propel integrationfor Silex.", 5 | "keywords": ["silex", "propel"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "William Durand", 10 | "email": "william.durand1@gmail.com" 11 | }, 12 | { 13 | "name": "Cristiano Cinotti", 14 | "email": "cristianocinotti@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "silex/silex": "~1.0", 19 | "propel/propel1": "~1.6" 20 | }, 21 | "autoload": { 22 | "psr-0": { "Propel\\Silex": "src" } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 14 | 15 | 16 | ./tests/Propel/ 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Propel/Silex/PropelServiceProvider.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class PropelServiceProvider implements ServiceProviderInterface 22 | { 23 | protected $alreadyInit = false; 24 | 25 | public function register(Application $app) 26 | { 27 | if (isset($app['propel.model_path']) && isset($app['propel.config_file'])) { 28 | $this->initPropel($app); 29 | } 30 | } 31 | 32 | public function boot(Application $app) 33 | { 34 | if (!$this->alreadyInit) { 35 | $this->initPropel($app); 36 | } 37 | } 38 | 39 | protected function initPropel(Application $app) 40 | { 41 | if (!class_exists('Propel')) { 42 | require_once $this->guessPropel($app); 43 | } 44 | 45 | $modelPath = $this->guessModelPath($app); 46 | $config = $this->guessConfigFile($app); 47 | 48 | \Propel::init($config); 49 | set_include_path($modelPath . PATH_SEPARATOR . get_include_path()); 50 | 51 | $this->alreadyInit = true; 52 | } 53 | 54 | protected function guessPropel(Application $app) 55 | { 56 | if (isset($app['propel.path'])) { 57 | $propel = $app['propel.path'] . '/Propel.php'; 58 | } else { 59 | if (!is_file($propel = realpath('./vendor/propel/propel1/runtime/lib/Propel.php'))) { 60 | $propel = realpath('./../vendor/propel/propel1/runtime/lib/Propel.php'); 61 | } 62 | } 63 | 64 | if (!is_file($propel)) { 65 | throw new \InvalidArgumentException('Unable to find Propel, did you set the "propel.path" parameter?'); 66 | } 67 | 68 | return $propel; 69 | } 70 | 71 | protected function guessModelPath(Application $app) 72 | { 73 | if (isset($app['propel.model_path'])) { 74 | $modelPath = $app['propel.model_path']; 75 | } else { 76 | $modelPath = './build/classes'; 77 | } 78 | 79 | if (!is_dir($modelPath)) { 80 | throw new \InvalidArgumentException('The given "propel.model_path" is not found.'); 81 | } 82 | 83 | return $modelPath; 84 | } 85 | 86 | protected function guessConfigFile(Application $app) 87 | { 88 | if (isset($app['propel.config_file'])) { 89 | $config = $app['propel.config_file']; 90 | } else { 91 | $currentDir = getcwd(); 92 | if (!@chdir(realpath('./build/conf'))) { 93 | throw new \InvalidArgumentException( 94 | 'Unable to guess the config file. Please, initialize the "propel.config_file" parameter.' 95 | ); 96 | } 97 | 98 | $files = glob('classmap*.php'); 99 | if (false === $files || 0 >= count($files)) { 100 | throw new \InvalidArgumentException( 101 | 'Unable to guess the config file. Please, initialize the "propel.config_file" parameter.' 102 | ); 103 | } 104 | 105 | $config = './build/conf/'.substr(strstr($files[0], '-'), 1); 106 | chdir($currentDir); 107 | } 108 | 109 | if (!is_file($config)) { 110 | throw new \InvalidArgumentException( 111 | 'Unable to guess the config file. Please, initialize the "propel.config_file" parameter.' 112 | ); 113 | } 114 | 115 | return $config; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /tests/Propel/Tests/Silex/PropelFixtures/FixtEmpty/build/classes/classes.txt: -------------------------------------------------------------------------------- 1 | This file has been added because git doesn't allow push of empty directories -------------------------------------------------------------------------------- /tests/Propel/Tests/Silex/PropelFixtures/FixtEmpty/build/conf/conf.txt: -------------------------------------------------------------------------------- 1 | This file has been added because git doesn't allow push of empty directories -------------------------------------------------------------------------------- /tests/Propel/Tests/Silex/PropelFixtures/FixtFull/build/classes/mynamespace/mynamespace.txt: -------------------------------------------------------------------------------- 1 | This file has been added because git doesn't allow push of empty directories -------------------------------------------------------------------------------- /tests/Propel/Tests/Silex/PropelFixtures/FixtFull/build/conf/classmap-myproject-conf.php: -------------------------------------------------------------------------------- 1 | 'model/Class1.php', 5 | 'model\\Class2' => 'model/Class2.php', 6 | 'model\\Class3' => 'model/Class3.php', 7 | ); 8 | -------------------------------------------------------------------------------- /tests/Propel/Tests/Silex/PropelFixtures/FixtFull/build/conf/myproject-conf.php: -------------------------------------------------------------------------------- 1 | 5 | array ( 6 | 'myproject' => 7 | array ( 8 | 'adapter' => 'sqlite', 9 | 'connection' => 10 | array ( 11 | 'dsn' => 'sqlite:', 12 | ), 13 | ), 14 | 'default' => 'myproject', 15 | ), 16 | 'generator_version' => '1.6.4-dev', 17 | ); 18 | $conf['classmap'] = include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classmap-myproject-conf.php'); 19 | return $conf; 20 | -------------------------------------------------------------------------------- /tests/Propel/Tests/Silex/PropelServiceProviderTest.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class PropelServiceProviderTest extends \PHPUnit_Framework_TestCase 22 | { 23 | public function setUp() 24 | { 25 | if (!class_exists('\Propel')) { 26 | $this->markTestSkipped('Propel has to be installed.'); 27 | } 28 | } 29 | 30 | public function testRegisterWithProperties() 31 | { 32 | $app = new Application(); 33 | $app->register(new PropelServiceProvider(), array( 34 | 'propel.path' => __DIR__ . '/../../../../vendor/propel/propel1/runtime/lib', 35 | 'propel.config_file' => __DIR__ . '/PropelFixtures/FixtFull/build/conf/myproject-conf.php', 36 | 'propel.model_path' => __DIR__ . '/PropelFixtures/FixtFull/build/classes', 37 | )); 38 | 39 | $this->assertTrue(class_exists('Propel'), 'Propel class does not exist.'); 40 | $this->assertGreaterThan(strpos(get_include_path(), $app['propel.model_path']), 1); 41 | } 42 | 43 | public function testRegisterDefaults() 44 | { 45 | $current = getcwd(); 46 | chdir(__DIR__.'/PropelFixtures/FixtFull'); 47 | 48 | $app = new Application(); 49 | $app->register(new PropelServiceProvider(), array( 50 | 'propel.path' => __DIR__ . '/../../../../vendor/propel/propel1/runtime/lib', 51 | )); 52 | 53 | $this->assertTrue(class_exists('Propel'), 'Propel class does not exist.'); 54 | $this->assertGreaterThan(strpos(get_include_path(), './build/classes'), 1); 55 | 56 | chdir($current); 57 | } 58 | 59 | /** 60 | * @expectedException InvalidArgumentException 61 | * @expectedExceptionMessage Unable to guess the config file. Please, initialize the "propel.config_file" parameter. 62 | */ 63 | public function testConfigFilePropertyNotInitialized() 64 | { 65 | $app = new Application(); 66 | $app->register(new PropelServiceProvider(), array( 67 | 'propel.path' => __DIR__.'/../../../../vendor/propel/propel1/runtime/lib', 68 | 'propel.model_path' => __DIR__.'/PropelFixtures/FixtFull/build/classes', 69 | )); 70 | $app->boot(); 71 | } 72 | 73 | /** 74 | * @expectedException InvalidArgumentException 75 | * @expectedExceptionMessage The given "propel.model_path" is not found. 76 | */ 77 | public function testWrongModelPath() 78 | { 79 | $app = new Application(); 80 | $app->register(new PropelServiceProvider(), array( 81 | 'propel.path' => __DIR__ . '/../../../../vendor/propel/propel1/runtime/lib', 82 | 'propel.config_file' => __DIR__ . '/PropelFixtures/FixtFull/build/conf/myproject-conf.php', 83 | 'propel.model_path' => __DIR__ . '/wrongDir/build/classes', 84 | )); 85 | $app->boot(); 86 | } 87 | 88 | public function testWrongConfigFile() 89 | { 90 | $current = getcwd(); 91 | chdir(__DIR__.'/PropelFixtures/FixtEmpty'); 92 | $app = new Application(); 93 | $app->register(new PropelServiceProvider(), array( 94 | 'propel.path' => __DIR__.'/../../../../vendor/propel/propel1/runtime/lib', 95 | 'propel.model_path' => __DIR__.'/PropelFixtures/FixtFull/build/classes', 96 | )); 97 | 98 | try { 99 | $app->boot(); 100 | $this->fail('An expected InvalidArgumentException has not been raised'); 101 | } catch(\InvalidArgumentException $e) { 102 | $this->assertTrue(true); 103 | } 104 | 105 | chdir($current); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | register('Propel', __DIR__.'/../src'); 5 | 6 | require_once __DIR__.'/../vendor/propel/propel1/runtime/lib/Propel.php'; 7 | --------------------------------------------------------------------------------