Skip to content

Commit 9dd873f

Browse files
committed
Releasing v1.0.0
- Breaks backwards-compatibility with controller configuration, it's now setup to work the same way as services.php
1 parent 4f962c1 commit 9dd873f

File tree

8 files changed

+58
-77
lines changed

8 files changed

+58
-77
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "studionone/flint",
3+
"version": "1.0.0",
34
"repositories": [
45
{
56
"type": "vcs",

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function loadConfig($configFile)
4949

5050
public function loadControllers($controllerFile = '')
5151
{
52+
$this->register(new ServiceControllerServiceProvider());
53+
5254
if ($controllerFile === '') {
5355
$controllerFile = $this->getAppConfig()['core']['configDir'] . $this->getAppConfig()['core']['controllersFile'];
5456
}
@@ -62,22 +64,6 @@ public function loadControllers($controllerFile = '')
6264
return $this;
6365
}
6466

65-
public function configureControllers()
66-
{
67-
$this->register(new ServiceControllerServiceProvider());
68-
69-
foreach ($this->getControllers() as $name => $callable) {
70-
if (! is_callable($callable)) {
71-
throw new InvalidControllerException('Controller `'.$name.'` is not a callable');
72-
}
73-
74-
$app = $this;
75-
$app[$name . '.controller'] = $app->share($callable);
76-
}
77-
78-
return $this;
79-
}
80-
8167
public function configureRoutes()
8268
{
8369
$routesFile = $this->getAppConfig()['core']['configDir'] . $this->getAppConfig()['core']['routesFile'];
@@ -93,7 +79,7 @@ public function configureServices()
9379
$servicesFile = $this->getAppConfig()['core']['configDir'] . $this->getAppConfig()['core']['servicesFile'];
9480

9581
$serviceParser = ServiceParser::getInstance($servicesFile);
96-
$serviceParser->loadServices()->parse();
82+
$serviceParser->loadServices()->loadControllers($this->getControllers())->parse();
9783

9884
// Sets up the Validator service provider
9985
$this->register(new ValidatorServiceProvider());
@@ -105,9 +91,8 @@ public function run(\Symfony\Component\HttpFoundation\Request $request = NULL)
10591
{
10692
$serviceOverride = null;
10793

108-
$this->configureServices()
109-
->loadControllers()
110-
->configureControllers()
94+
$this->loadControllers()
95+
->configureServices()
11196
->configureRoutes();
11297

11398
if (method_exists($this, 'init')) {

src/ServiceParser.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ServiceParser
1919

2020
protected $servicesFile = '';
2121
protected $services = null;
22+
protected $controllers = null;
2223

2324
public function __construct($servicesFile)
2425
{
@@ -38,16 +39,48 @@ public function loadServices()
3839
return $this;
3940
}
4041

42+
public function loadControllers(array $controllers)
43+
{
44+
// Munge the controllers
45+
$fixed = [];
46+
foreach ($controllers as $name => $value) {
47+
// Make sure it's shared
48+
if (array_key_exists('share', $value) === false
49+
|| $value['share'] !== true) {
50+
$value['share'] = true;
51+
}
52+
53+
// Ensure it's name ends with 'controller'
54+
if (substr($name, strlen($name) - strlen('.controller')) !== '.controller') {
55+
throw new InvalidControllersFileException('ControllerService names must end in ".controller": ' . $name);
56+
}
57+
58+
$fixed[$name] = $value;
59+
}
60+
61+
$this->setControllers($fixed);
62+
63+
return $this;
64+
}
65+
4166
// TODO: Add in support for "shared" and "protected" callbacks
4267
public function parse()
4368
{
4469
$app = \Flint\App::getInstance();
45-
$raw = $this->getServices();
70+
$services = $this->getServices();
71+
$controllers = $this->getControllers();
4672

47-
if ($raw === null) {
73+
if ($services === null) {
4874
throw new \ErrorException('Trying to parse loaded services before loading the file.');
4975
}
5076

77+
// Append the controllers onto the service definitions
78+
if ($controllers === null) {
79+
$controllers = [];
80+
}
81+
82+
$raw = array_merge($services, $controllers);
83+
5184
foreach ($raw as $name => $values) {
5285
if (isset($values['shared']) && $values['shared'] === true) {
5386
// Shared service; ie: saves a single copy of the object passed in

tests/data/controllers.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

33
return [
4-
'fake' => function() {
5-
return new FakeController();
6-
}
4+
'fake.controller' => [
5+
'class' => 'FakeController',
6+
'arguments' => []
7+
]
78
];

tests/unit/AppTest.php

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,54 +48,11 @@ public function testConfigOverride()
4848
$this->assertArrayHasKey('configDir', $config['core']);
4949
}
5050

51-
/**
52-
* @expectedException \Flint\Exception\InvalidControllersFileException
53-
*/
54-
public function testInvalidControllersFileThrowsException()
55-
{
56-
$this->config['core']['controllersFile'] = 'abcd12345.php';
57-
58-
$app = App::getInstance($this->config);
59-
$app->loadControllers();
60-
}
61-
62-
public function testLoadingRealControllers()
63-
{
64-
$app = App::getInstance($this->config);
65-
66-
$controllers = $app->loadControllers()->getControllers();
67-
68-
$this->assertTrue(is_array($controllers));
69-
$this->assertArrayHasKey('fake', $controllers);
70-
$this->assertArrayNotHasKey('testing', $controllers);
71-
}
72-
73-
/**
74-
* @expectedException \Flint\Exception\InvalidControllerException
75-
*/
76-
public function testInvalidControllerThrowsException()
77-
{
78-
$this->config['core']['controllersFile'] = '/controllers.invalid.php';
79-
$app = App::getInstance($this->config);
80-
81-
$app->loadControllers()
82-
->configureControllers();
83-
}
84-
85-
public function testControllersLoadedIntoSilex()
86-
{
87-
$app = App::getInstance($this->config);
88-
89-
$app->loadControllers()
90-
->configureControllers();
91-
92-
$this->assertArrayHasKey('fake.controller', $app);
93-
}
94-
9551
public function testServicesLoadedIntoPimple()
9652
{
9753
$app = App::getInstance($this->config);
9854

55+
$app->loadControllers();
9956
$app->configureServices();
10057

10158
$this->assertArrayHasKey('Fake', $app);
@@ -109,7 +66,7 @@ public function testRoutesAreLoadedInCorrectly()
10966
$app = App::getInstance($this->config);
11067

11168
$app->loadControllers()
112-
->configureControllers()
69+
->configureServices()
11370
->configureRoutes();
11471

11572
// Now see if the routes are loaded

tests/unit/RouteParserTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function setUp()
1616
'core' => [
1717
'configDir' => __DIR__ . '/../data',
1818
'controllersFile' => '/controllers.php',
19+
'servicesFile' => '/services.php',
1920
'routesFile' => '/routes.php'
2021
]
2122
];
@@ -56,7 +57,7 @@ public function testRouteParsing()
5657
$parser = new RouteParser($file);
5758

5859
$this->app->loadControllers()
59-
->configureControllers();
60+
->configureServices();
6061

6162
$parser->loadRoutes()->parse();
6263

@@ -117,7 +118,7 @@ public function testConverterLoadedAndRuns()
117118
$parser = new RouteParser($file);
118119

119120
$this->app->loadControllers()
120-
->configureControllers();
121+
->configureServices();
121122

122123
$parser->loadRoutes()->parse();
123124

tests/unit/ServiceParserTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
class ServiceParserTest extends \PHPUnit_Framework_TestCase
1010
{
1111
private $fakeConfig1 = [ 'hello' => 'world' ];
12-
private $fakeConfig2 = [
12+
private $fakeConfig2 = [];
1313

14-
];
14+
public function setUp()
15+
{
16+
ServiceParser::destroyInstance();
17+
}
1518

1619
public function tearDown()
1720
{
@@ -23,7 +26,7 @@ public function testCorrectInitialisation()
2326
{
2427
$parser = ServiceParser::getInstance('fakefile.php');
2528

26-
$this->assertTrue('fakefile.php' === $parser->getServicesFile());
29+
$this->assertEquals('fakefile.php', $parser->getServicesFile());
2730
}
2831

2932
public function testLoadServicesFileIntoParser()

0 commit comments

Comments
 (0)