Browse files

Merge pull request #5 from cristianoc72/master

Added the ability of automatic registration of costum model namespaces
  • Loading branch information...
2 parents 2579518 + f1360c3 commit a67a9407a0d82b8e6110105438886444bd03b6e1 @willdurand willdurand committed Mar 24, 2012
View
26 src/Propel/Silex/PropelServiceProvider.php
@@ -33,12 +33,16 @@ public function register(Application $app)
} else {
$modelPath = realpath('./build/classes');
}
+
+ if (!is_dir($modelPath)) {
+ throw new \InvalidArgumentException(__CLASS__.': please, initialize the "propel.model_path" parameter (did you already generate your model?)');
+ }
if (isset($app['propel.config_file'])) {
$config = $app['propel.config_file'];
} else {
$currentDir = getcwd();
- if (!chdir(realpath('./build/conf'))) {
+ if (!@chdir(realpath('./build/conf'))) {
throw new \InvalidArgumentException(__CLASS__.': please, initialize the "propel.config_file" parameter.');
}
@@ -54,7 +58,25 @@ public function register(Application $app)
if (isset($app['propel.internal_autoload']) && true === $app['propel.internal_autoload']) {
set_include_path($modelPath.PATH_SEPARATOR.get_include_path());
} else {
- $app['autoloader']->registerNamespace('model', $modelPath);
+ //model namespaces are subdir of $modelPath directory
+ $dir = new \DirectoryIterator($modelPath);
+
+ //Unfortunately DirectoryIterator count() method is not always implemented, so we need a boolean
+ //to check if $modelPath dir has at least one subdir, otherwise te model has not yet been generated or
+ //$modelPath contains a wrong value.
+ $built = false;
+ foreach ($dir as $fileInfo) {
+ if ($fileInfo->isDir()) {
+ if (!$fileInfo->isDot()) {
+ $built = true;
+ $app['autoloader']->registerNamespace($fileInfo->getFilename(), $modelPath);
+ }
+ }
+ }
+
+ if (!$built) {
+ throw new \InvalidArgumentException(__CLASS__.': '.$modelPath.' has no subdir. May be "propel.model_path" value is wrong or you didn\'t yet generate your model.');
+ }
}
if (!class_exists('Propel')) {
View
0 ...es/build/conf/classmap-myproject-conf.php → ...ll/build/conf/classmap-myproject-conf.php
File renamed without changes.
View
0 ...pelFixtures/build/conf/myproject-conf.php → ...es/FixtFull/build/conf/myproject-conf.php
File renamed without changes.
View
71 tests/Propel/Tests/Silex/PropelServiceProviderTest.php
@@ -26,14 +26,14 @@ public function setUp()
$this->markTestSkipped('The Propel submodule is not installed.');
}
}
-
+
public function testRegisterWithProperties()
{
$app = new Application();
$app->register(new PropelServiceProvider(), array(
'propel.path' => __DIR__ . '/../../../../vendor/propel/runtime/lib',
- 'propel.config_file' => __DIR__ . '/PropelFixtures/build/conf/myproject-conf.php',
- 'propel.model_path' => __DIR__ . '/PropelFixtures/build/classes',
+ 'propel.config_file' => __DIR__ . '/PropelFixtures/FixtFull/build/conf/myproject-conf.php',
+ 'propel.model_path' => __DIR__ . '/PropelFixtures/FixtFull/build/classes',
));
$this->assertTrue(class_exists('Propel'));
@@ -43,7 +43,7 @@ public function testRegisterWithProperties()
public function testRegisterDefaults()
{
$current = getcwd();
- chdir(__DIR__.'/PropelFixtures');
+ chdir(__DIR__.'/PropelFixtures/FixtFull');
$app = new Application();
$app->register(new PropelServiceProvider());
@@ -58,12 +58,71 @@ public function testRegisterInternalAutoload()
$app = new Application();
$app->register(new PropelServiceProvider(), array(
'propel.path' => __DIR__.'/../../../../vendor/propel/runtime/lib',
- 'propel.config_file' => __DIR__.'/PropelFixtures/build/conf/myproject-conf.php',
- 'propel.model_path' => __DIR__.'/PropelFixtures/build/classes',
+ 'propel.config_file' => __DIR__.'/PropelFixtures/FixtFull/build/conf/myproject-conf.php',
+ 'propel.model_path' => __DIR__.'/PropelFixtures/FixtFull/build/classes',
'propel.internal_autoload' => true,
));
$this->assertTrue(class_exists('Propel'), 'Propel class does not exist.');
$this->assertGreaterThan(strpos(get_include_path(), $app['propel.model_path']), 1);
}
+
+ /**
+ * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage Propel\Silex\PropelServiceProvider: please, initialize the "propel.model_path" parameter (did you already generate your model?)
+ */
+ public function testModelPathPropertyNotInitialized()
+ {
+ $app = new Application();
+ $app->register(new PropelServiceProvider());
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage Propel\Silex\PropelServiceProvider: please, initialize the "propel.config_file" parameter.
+ */
+ public function testConfigFilePropertyNotInitialized()
+ {
+ $app = new Application();
+ $app->register(new PropelServiceProvider(), array(
+ 'propel.path' => __DIR__.'/../../../../vendor/propel/runtime/lib',
+ 'propel.model_path' => __DIR__.'/PropelFixtures/FixtFull/build/classes',
+ ));
+ }
+
+ public function testWrongConfigFile()
+ {
+ $current = getcwd();
+ try
+ {
+ chdir(__DIR__.'/PropelFixtures/FixtEmpty');
+ $app = new Application();
+ $app->register(new PropelServiceProvider(), array(
+ 'propel.path' => __DIR__.'/../../../../vendor/propel/runtime/lib',
+ 'propel.model_path' => __DIR__.'/PropelFixtures/FixtFull/build/classes',
+ ));
+ }
+ catch(\InvalidArgumentException $e)
+ {
+ chdir($current);
+ return;
+ }
+
+ chdir($current);
+ $this->failed('An expected InvalidArgumentException has not been raised');
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testNoNamespace()
+ {
+ $app = new Application();
+ $app->register(new PropelServiceProvider(), array(
+ 'propel.path' => __DIR__.'/../../../../vendor/propel/runtime/lib',
+ 'propel.model_path' => __DIR__.'/PropelFixtures/FixtEmpty/build/classes',
+ 'propel.config_file' => __DIR__.'/PropelFixtures/FixtFull/build/conf/myproject-conf.php',
+ ));
+ }
+
}

0 comments on commit a67a940

Please sign in to comment.