Skip to content

Commit

Permalink
Merge pull request propelorm#5 from cristianoc72/master
Browse files Browse the repository at this point in the history
Added the ability of automatic registration of costum model namespaces
  • Loading branch information
willdurand committed Mar 24, 2012
2 parents 2579518 + f1360c3 commit a67a940
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
26 changes: 24 additions & 2 deletions src/Propel/Silex/PropelServiceProvider.php
Expand Up @@ -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.');
}

Expand All @@ -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')) {
Expand Down
71 changes: 65 additions & 6 deletions tests/Propel/Tests/Silex/PropelServiceProviderTest.php
Expand Up @@ -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'));
Expand All @@ -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());
Expand All @@ -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.