Skip to content

Commit 1f84e57

Browse files
committed
Uniform Code Style (PSR-2)
1 parent a5d6842 commit 1f84e57

File tree

5 files changed

+179
-192
lines changed

5 files changed

+179
-192
lines changed

config/definitions.php

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,67 @@
22

33
return
44
[
5-
/**
6-
* Monolog
7-
*
8-
* @link https://github.com/Seldaek/monolog
9-
*/
10-
Psr\Log\LoggerInterface::class => function($container)
11-
{
12-
$file = __DIR__ . '/../app.log';
5+
/**
6+
* Monolog
7+
*
8+
* @link https://github.com/Seldaek/monolog
9+
*/
10+
Psr\Log\LoggerInterface::class => function ($container) {
11+
$handler = new Monolog\Handler\StreamHandler(__DIR__ . '/../app.log');
12+
$handler->setFormatter(new Monolog\Formatter\LineFormatter);
1313

14-
$handler = new Monolog\Handler\StreamHandler($file);
15-
$handler->setFormatter(new Monolog\Formatter\LineFormatter);
14+
$logger = new Monolog\Logger('app');
15+
$logger->pushHandler($handler);
1616

17-
$logger = new Monolog\Logger('app');
18-
$logger->pushHandler($handler);
17+
return $logger;
18+
},
1919

20-
return $logger;
21-
},
20+
/**
21+
* Sunrise HTTP Router
22+
*
23+
* @link https://github.com/sunrise-php/http-router
24+
* @link https://github.com/sunrise-php/http-router-annotations-support
25+
* @link https://github.com/middlewares/utils/pull/11
26+
*/
27+
Sunrise\Http\Router\RouterInterface::class => function ($container) {
28+
Middlewares\Utils\Factory::setResponseFactory(new Sunrise\Http\Factory\ResponseFactory);
29+
Middlewares\Utils\Factory::setServerRequestFactory(new Sunrise\Http\Factory\ServerRequestFactory);
30+
Middlewares\Utils\Factory::setStreamFactory(new Sunrise\Http\Factory\StreamFactory);
31+
Middlewares\Utils\Factory::setUriFactory(new Sunrise\Http\Factory\UriFactory);
2232

23-
/**
24-
* Sunrise HTTP Router
25-
*
26-
* @link https://github.com/sunrise-php/http-router
27-
* @link https://github.com/sunrise-php/http-router-annotations-support
28-
* @link https://github.com/middlewares/utils/pull/11
29-
*/
30-
Sunrise\Http\Router\RouterInterface::class => function($container)
31-
{
32-
Middlewares\Utils\Factory::setResponseFactory(new Sunrise\Http\Factory\ResponseFactory);
33-
Middlewares\Utils\Factory::setServerRequestFactory(new Sunrise\Http\Factory\ServerRequestFactory);
34-
Middlewares\Utils\Factory::setStreamFactory(new Sunrise\Http\Factory\StreamFactory);
35-
Middlewares\Utils\Factory::setUriFactory(new Sunrise\Http\Factory\UriFactory);
33+
$router = new Sunrise\Http\Router\Router();
34+
$router->addMiddleware(new Middlewares\Whoops);
35+
$router->addMiddleware(new Middlewares\ResponseTime);
36+
$router->addMiddleware(new Middlewares\UrlEncodePayload);
37+
$router->addMiddleware(new Middlewares\JsonPayload);
3638

37-
$router = new Sunrise\Http\Router\Router();
38-
$loader = new Sunrise\Http\Router\AnnotationRouteLoader();
39+
$loader = new Sunrise\Http\Router\AnnotationRouteLoader();
40+
$routes = $loader->load(__DIR__ . '/../src/Http/Controller', [$container, 'get']);
41+
$router->addRoutes($routes);
3942

40-
$router->addMiddleware(new Middlewares\Whoops);
41-
$router->addMiddleware(new Middlewares\ResponseTime);
42-
$router->addMiddleware(new Middlewares\UrlEncodePayload);
43-
$router->addMiddleware(new Middlewares\JsonPayload);
43+
return $router;
44+
},
4445

45-
$routes = $loader->load(__DIR__ . '/../src/Http/Controller', [$container, 'get']);
46-
$router->addRoutes($routes);
46+
/**
47+
* Doctrine Entity Manager
48+
*
49+
* @link https://www.doctrine-project.org/
50+
* @link https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/advanced-configuration.html
51+
* @link http://srcmvn.com/blog/2011/11/10/doctrine-dbal-query-logging-with-monolog-in-silex
52+
*/
53+
Doctrine\ORM\EntityManager::class => function ($container) {
54+
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');
4755

48-
return $router;
49-
},
56+
$debug = in_array($container->get('env'), ['local', 'development', 'test']);
5057

51-
/**
52-
* Doctrine Entity Manager
53-
*
54-
* @link https://www.doctrine-project.org/
55-
*/
56-
Doctrine\ORM\EntityManager::class => function($container)
57-
{
58-
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');
58+
$config = Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
59+
[__DIR__ . '/../src/Entity'],
60+
$debug,
61+
null,
62+
null,
63+
false
64+
);
5965

60-
$debug = in_array($container->get('env'), ['local', 'development']);
61-
62-
$config = Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration([__DIR__ . '/../src/Entity'], $debug, null, null, false);
63-
64-
$manager = Doctrine\ORM\EntityManager::create($container->get('database'), $config);
65-
66-
return $manager;
67-
},
66+
return Doctrine\ORM\EntityManager::create($container->get('database'), $config);
67+
},
6868
];

config/environment.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22

33
return
44
[
5-
/**
6-
* The application environment
7-
*
8-
* MUST contain: "local", development", "stage" or "production".
9-
*
10-
* @var string
11-
*/
12-
'env' => 'development',
5+
/**
6+
* The application environment
7+
*
8+
* MUST contain: "local", "development", "test", "stage" or "production".
9+
*
10+
* @var string
11+
*/
12+
'env' => 'local',
1313

14-
/**
15-
* Database Connection Configuration
16-
*
17-
* @var array
18-
*
19-
* @link https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/configuration.html
20-
*/
21-
'database' =>
22-
[
23-
'driver' => 'pdo_mysql',
24-
'host' => '127.0.0.1',
25-
'port' => 3306,
26-
'user' => 'rgmHtHYsPK6q7CQb',
27-
'password' => 'jHsmw2D8HjQbLUYD',
28-
'dbname' => 'app',
29-
'charset' => 'utf8mb4',
30-
],
14+
/**
15+
* Database Connection Configuration
16+
*
17+
* @var array
18+
*
19+
* @link https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/configuration.html
20+
*/
21+
'database' => [
22+
'driver' => 'pdo_mysql',
23+
'host' => '127.0.0.1',
24+
'port' => 3306,
25+
'user' => 'rgmHtHYsPK6q7CQb',
26+
'password' => 'jHsmw2D8HjQbLUYD',
27+
'dbname' => 'app',
28+
'charset' => 'utf8mb4',
29+
],
3130
];

src/Entity/AbstractEntity.php

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,37 @@
1212
abstract class AbstractEntity
1313
{
1414

15-
/**
16-
* The entity validator
17-
*
18-
* @var null|ValidatorInterface
19-
*/
20-
protected $validator;
21-
22-
/**
23-
* Gets the entity validator
24-
*
25-
* @return ValidatorInterface
26-
*/
27-
public function getValidator() : ValidatorInterface
28-
{
29-
if (empty($this->validator))
30-
{
31-
$builder = Validation::createValidatorBuilder();
32-
33-
$builder->enableAnnotationMapping();
34-
35-
$this->validator = $builder->getValidator();
36-
}
37-
38-
return $this->validator;
39-
}
40-
41-
/**
42-
* Validates the entity
43-
*
44-
* @return ConstraintViolationListInterface
45-
*/
46-
public function validate() : ConstraintViolationListInterface
47-
{
48-
$validator = $this->getValidator();
49-
50-
return $validator->validate($this);
51-
}
15+
/**
16+
* The entity validator
17+
*
18+
* @var null|ValidatorInterface
19+
*/
20+
protected $validator;
21+
22+
/**
23+
* Gets the entity validator
24+
*
25+
* @return ValidatorInterface
26+
*/
27+
public function getValidator() : ValidatorInterface
28+
{
29+
if (empty($this->validator)) {
30+
$this->validator = Validation::createValidatorBuilder()
31+
->enableAnnotationMapping()
32+
->getValidator();
33+
}
34+
35+
return $this->validator;
36+
}
37+
38+
/**
39+
* Validates the entity
40+
*
41+
* @return ConstraintViolationListInterface
42+
*/
43+
public function validate() : ConstraintViolationListInterface
44+
{
45+
return $this->getValidator()
46+
->validate($this);
47+
}
5248
}

src/Http/App.php

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,70 +18,66 @@
1818
class App
1919
{
2020

21-
/**
22-
* The application router
23-
*
24-
* @var RouterInterface
25-
*
26-
* @Inject
27-
*/
28-
protected $router;
21+
/**
22+
* The application router
23+
*
24+
* @var RouterInterface
25+
*
26+
* @Inject
27+
*/
28+
protected $router;
2929

30-
/**
31-
* Runs the application
32-
*
33-
* @param ServerRequestInterface $request
34-
*
35-
* @return void
36-
*/
37-
public function run(ServerRequestInterface $request) : void
38-
{
39-
try
40-
{
41-
$response = $this->router->handle($request);
42-
}
43-
catch (MethodNotAllowedException $e)
44-
{
45-
$response = (new ResponseFactory)->createResponse(405)
46-
->withHeader('allow', implode(', ', $e->getAllowedMethods()));
30+
/**
31+
* Runs the application
32+
*
33+
* @param ServerRequestInterface $request
34+
*
35+
* @return void
36+
*/
37+
public function run(ServerRequestInterface $request) : void
38+
{
39+
try {
40+
$response = $this->router->handle($request);
41+
} catch (RouteNotFoundException $e) {
42+
$response = (new ResponseFactory)->createResponse(404);
43+
$response->getBody()->write($response->getReasonPhrase());
44+
} catch (MethodNotAllowedException $e) {
45+
$response = (new ResponseFactory)->createResponse(405)
46+
->withHeader('allow', implode(', ', $e->getAllowedMethods()));
47+
$response->getBody()->write($response->getReasonPhrase());
48+
}
4749

48-
$response->getBody()->write($response->getReasonPhrase());
49-
}
50-
catch (RouteNotFoundException $e)
51-
{
52-
$response = (new ResponseFactory)->createResponse(404);
50+
$this->emit($response);
51+
}
5352

54-
$response->getBody()->write($response->getReasonPhrase());
55-
}
53+
/**
54+
* Emits the given response
55+
*
56+
* @param ResponseInterface $response
57+
*
58+
* @return void
59+
*/
60+
public function emit(ResponseInterface $response) : void
61+
{
62+
$headers = $response->getHeaders();
5663

57-
$this->emit($response);
58-
}
64+
foreach ($headers as $name => $values) {
65+
foreach ($values as $value) {
66+
\header(\sprintf(
67+
'%s: %s',
68+
$name,
69+
$value
70+
), false);
71+
}
72+
}
5973

60-
/**
61-
* Emits the given response
62-
*
63-
* @param ResponseInterface $response
64-
*
65-
* @return void
66-
*/
67-
public function emit(ResponseInterface $response) : void
68-
{
69-
$headers = $response->getHeaders();
74+
\header(\sprintf(
75+
'HTTP/%s %d %s',
76+
$response->getProtocolVersion(),
77+
$response->getStatusCode(),
78+
$response->getReasonPhrase()
79+
), true);
7080

71-
foreach ($headers as $name => $values)
72-
{
73-
foreach ($values as $value)
74-
{
75-
\header(\sprintf('%s: %s', $name, $value), false);
76-
}
77-
}
78-
79-
\header(\sprintf('HTTP/%s %d %s',
80-
$response->getProtocolVersion(),
81-
$response->getStatusCode(),
82-
$response->getReasonPhrase()
83-
), true);
84-
85-
echo $response->getBody();
86-
}
81+
echo $response->getBody();
82+
}
8783
}

0 commit comments

Comments
 (0)