Skip to content

Commit a67a940

Browse files
committed
Merge pull request #5 from cristianoc72/master
Added the ability of automatic registration of costum model namespaces
2 parents 2579518 + f1360c3 commit a67a940

File tree

4 files changed

+89
-8
lines changed

4 files changed

+89
-8
lines changed

src/Propel/Silex/PropelServiceProvider.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ public function register(Application $app)
3333
} else {
3434
$modelPath = realpath('./build/classes');
3535
}
36+
37+
if (!is_dir($modelPath)) {
38+
throw new \InvalidArgumentException(__CLASS__.': please, initialize the "propel.model_path" parameter (did you already generate your model?)');
39+
}
3640

3741
if (isset($app['propel.config_file'])) {
3842
$config = $app['propel.config_file'];
3943
} else {
4044
$currentDir = getcwd();
41-
if (!chdir(realpath('./build/conf'))) {
45+
if (!@chdir(realpath('./build/conf'))) {
4246
throw new \InvalidArgumentException(__CLASS__.': please, initialize the "propel.config_file" parameter.');
4347
}
4448

@@ -54,7 +58,25 @@ public function register(Application $app)
5458
if (isset($app['propel.internal_autoload']) && true === $app['propel.internal_autoload']) {
5559
set_include_path($modelPath.PATH_SEPARATOR.get_include_path());
5660
} else {
57-
$app['autoloader']->registerNamespace('model', $modelPath);
61+
//model namespaces are subdir of $modelPath directory
62+
$dir = new \DirectoryIterator($modelPath);
63+
64+
//Unfortunately DirectoryIterator count() method is not always implemented, so we need a boolean
65+
//to check if $modelPath dir has at least one subdir, otherwise te model has not yet been generated or
66+
//$modelPath contains a wrong value.
67+
$built = false;
68+
foreach ($dir as $fileInfo) {
69+
if ($fileInfo->isDir()) {
70+
if (!$fileInfo->isDot()) {
71+
$built = true;
72+
$app['autoloader']->registerNamespace($fileInfo->getFilename(), $modelPath);
73+
}
74+
}
75+
}
76+
77+
if (!$built) {
78+
throw new \InvalidArgumentException(__CLASS__.': '.$modelPath.' has no subdir. May be "propel.model_path" value is wrong or you didn\'t yet generate your model.');
79+
}
5880
}
5981

6082
if (!class_exists('Propel')) {

tests/Propel/Tests/Silex/PropelFixtures/build/conf/classmap-myproject-conf.php renamed to tests/Propel/Tests/Silex/PropelFixtures/FixtFull/build/conf/classmap-myproject-conf.php

File renamed without changes.

tests/Propel/Tests/Silex/PropelFixtures/build/conf/myproject-conf.php renamed to tests/Propel/Tests/Silex/PropelFixtures/FixtFull/build/conf/myproject-conf.php

File renamed without changes.

tests/Propel/Tests/Silex/PropelServiceProviderTest.php

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ public function setUp()
2626
$this->markTestSkipped('The Propel submodule is not installed.');
2727
}
2828
}
29-
29+
3030
public function testRegisterWithProperties()
3131
{
3232
$app = new Application();
3333
$app->register(new PropelServiceProvider(), array(
3434
'propel.path' => __DIR__ . '/../../../../vendor/propel/runtime/lib',
35-
'propel.config_file' => __DIR__ . '/PropelFixtures/build/conf/myproject-conf.php',
36-
'propel.model_path' => __DIR__ . '/PropelFixtures/build/classes',
35+
'propel.config_file' => __DIR__ . '/PropelFixtures/FixtFull/build/conf/myproject-conf.php',
36+
'propel.model_path' => __DIR__ . '/PropelFixtures/FixtFull/build/classes',
3737
));
3838

3939
$this->assertTrue(class_exists('Propel'));
@@ -43,7 +43,7 @@ public function testRegisterWithProperties()
4343
public function testRegisterDefaults()
4444
{
4545
$current = getcwd();
46-
chdir(__DIR__.'/PropelFixtures');
46+
chdir(__DIR__.'/PropelFixtures/FixtFull');
4747

4848
$app = new Application();
4949
$app->register(new PropelServiceProvider());
@@ -58,12 +58,71 @@ public function testRegisterInternalAutoload()
5858
$app = new Application();
5959
$app->register(new PropelServiceProvider(), array(
6060
'propel.path' => __DIR__.'/../../../../vendor/propel/runtime/lib',
61-
'propel.config_file' => __DIR__.'/PropelFixtures/build/conf/myproject-conf.php',
62-
'propel.model_path' => __DIR__.'/PropelFixtures/build/classes',
61+
'propel.config_file' => __DIR__.'/PropelFixtures/FixtFull/build/conf/myproject-conf.php',
62+
'propel.model_path' => __DIR__.'/PropelFixtures/FixtFull/build/classes',
6363
'propel.internal_autoload' => true,
6464
));
6565

6666
$this->assertTrue(class_exists('Propel'), 'Propel class does not exist.');
6767
$this->assertGreaterThan(strpos(get_include_path(), $app['propel.model_path']), 1);
6868
}
69+
70+
/**
71+
* @expectedException InvalidArgumentException
72+
* @expectedExceptionMessage Propel\Silex\PropelServiceProvider: please, initialize the "propel.model_path" parameter (did you already generate your model?)
73+
*/
74+
public function testModelPathPropertyNotInitialized()
75+
{
76+
$app = new Application();
77+
$app->register(new PropelServiceProvider());
78+
}
79+
80+
/**
81+
* @expectedException InvalidArgumentException
82+
* @expectedExceptionMessage Propel\Silex\PropelServiceProvider: please, initialize the "propel.config_file" parameter.
83+
*/
84+
public function testConfigFilePropertyNotInitialized()
85+
{
86+
$app = new Application();
87+
$app->register(new PropelServiceProvider(), array(
88+
'propel.path' => __DIR__.'/../../../../vendor/propel/runtime/lib',
89+
'propel.model_path' => __DIR__.'/PropelFixtures/FixtFull/build/classes',
90+
));
91+
}
92+
93+
public function testWrongConfigFile()
94+
{
95+
$current = getcwd();
96+
try
97+
{
98+
chdir(__DIR__.'/PropelFixtures/FixtEmpty');
99+
$app = new Application();
100+
$app->register(new PropelServiceProvider(), array(
101+
'propel.path' => __DIR__.'/../../../../vendor/propel/runtime/lib',
102+
'propel.model_path' => __DIR__.'/PropelFixtures/FixtFull/build/classes',
103+
));
104+
}
105+
catch(\InvalidArgumentException $e)
106+
{
107+
chdir($current);
108+
return;
109+
}
110+
111+
chdir($current);
112+
$this->failed('An expected InvalidArgumentException has not been raised');
113+
}
114+
115+
/**
116+
* @expectedException InvalidArgumentException
117+
*/
118+
public function testNoNamespace()
119+
{
120+
$app = new Application();
121+
$app->register(new PropelServiceProvider(), array(
122+
'propel.path' => __DIR__.'/../../../../vendor/propel/runtime/lib',
123+
'propel.model_path' => __DIR__.'/PropelFixtures/FixtEmpty/build/classes',
124+
'propel.config_file' => __DIR__.'/PropelFixtures/FixtFull/build/conf/myproject-conf.php',
125+
));
126+
}
127+
69128
}

0 commit comments

Comments
 (0)