Skip to content

Commit

Permalink
Uniform Code Style (PSR-2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Jan 14, 2019
1 parent a5d6842 commit 1f84e57
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 192 deletions.
106 changes: 53 additions & 53 deletions config/definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,67 @@

return
[
/**
* Monolog
*
* @link https://github.com/Seldaek/monolog
*/
Psr\Log\LoggerInterface::class => function($container)
{
$file = __DIR__ . '/../app.log';
/**
* Monolog
*
* @link https://github.com/Seldaek/monolog
*/
Psr\Log\LoggerInterface::class => function ($container) {
$handler = new Monolog\Handler\StreamHandler(__DIR__ . '/../app.log');
$handler->setFormatter(new Monolog\Formatter\LineFormatter);

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

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

return $logger;
},
/**
* Sunrise HTTP Router
*
* @link https://github.com/sunrise-php/http-router
* @link https://github.com/sunrise-php/http-router-annotations-support
* @link https://github.com/middlewares/utils/pull/11
*/
Sunrise\Http\Router\RouterInterface::class => function ($container) {
Middlewares\Utils\Factory::setResponseFactory(new Sunrise\Http\Factory\ResponseFactory);
Middlewares\Utils\Factory::setServerRequestFactory(new Sunrise\Http\Factory\ServerRequestFactory);
Middlewares\Utils\Factory::setStreamFactory(new Sunrise\Http\Factory\StreamFactory);
Middlewares\Utils\Factory::setUriFactory(new Sunrise\Http\Factory\UriFactory);

/**
* Sunrise HTTP Router
*
* @link https://github.com/sunrise-php/http-router
* @link https://github.com/sunrise-php/http-router-annotations-support
* @link https://github.com/middlewares/utils/pull/11
*/
Sunrise\Http\Router\RouterInterface::class => function($container)
{
Middlewares\Utils\Factory::setResponseFactory(new Sunrise\Http\Factory\ResponseFactory);
Middlewares\Utils\Factory::setServerRequestFactory(new Sunrise\Http\Factory\ServerRequestFactory);
Middlewares\Utils\Factory::setStreamFactory(new Sunrise\Http\Factory\StreamFactory);
Middlewares\Utils\Factory::setUriFactory(new Sunrise\Http\Factory\UriFactory);
$router = new Sunrise\Http\Router\Router();
$router->addMiddleware(new Middlewares\Whoops);
$router->addMiddleware(new Middlewares\ResponseTime);
$router->addMiddleware(new Middlewares\UrlEncodePayload);
$router->addMiddleware(new Middlewares\JsonPayload);

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

$router->addMiddleware(new Middlewares\Whoops);
$router->addMiddleware(new Middlewares\ResponseTime);
$router->addMiddleware(new Middlewares\UrlEncodePayload);
$router->addMiddleware(new Middlewares\JsonPayload);
return $router;
},

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

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

/**
* Doctrine Entity Manager
*
* @link https://www.doctrine-project.org/
*/
Doctrine\ORM\EntityManager::class => function($container)
{
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');
$config = Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
[__DIR__ . '/../src/Entity'],
$debug,
null,
null,
false
);

$debug = in_array($container->get('env'), ['local', 'development']);

$config = Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration([__DIR__ . '/../src/Entity'], $debug, null, null, false);

$manager = Doctrine\ORM\EntityManager::create($container->get('database'), $config);

return $manager;
},
return Doctrine\ORM\EntityManager::create($container->get('database'), $config);
},
];
49 changes: 24 additions & 25 deletions config/environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@

return
[
/**
* The application environment
*
* MUST contain: "local", development", "stage" or "production".
*
* @var string
*/
'env' => 'development',
/**
* The application environment
*
* MUST contain: "local", "development", "test", "stage" or "production".
*
* @var string
*/
'env' => 'local',

/**
* Database Connection Configuration
*
* @var array
*
* @link https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/configuration.html
*/
'database' =>
[
'driver' => 'pdo_mysql',
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'rgmHtHYsPK6q7CQb',
'password' => 'jHsmw2D8HjQbLUYD',
'dbname' => 'app',
'charset' => 'utf8mb4',
],
/**
* Database Connection Configuration
*
* @var array
*
* @link https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/configuration.html
*/
'database' => [
'driver' => 'pdo_mysql',
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'rgmHtHYsPK6q7CQb',
'password' => 'jHsmw2D8HjQbLUYD',
'dbname' => 'app',
'charset' => 'utf8mb4',
],
];
70 changes: 33 additions & 37 deletions src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,37 @@
abstract class AbstractEntity
{

/**
* The entity validator
*
* @var null|ValidatorInterface
*/
protected $validator;

/**
* Gets the entity validator
*
* @return ValidatorInterface
*/
public function getValidator() : ValidatorInterface
{
if (empty($this->validator))
{
$builder = Validation::createValidatorBuilder();

$builder->enableAnnotationMapping();

$this->validator = $builder->getValidator();
}

return $this->validator;
}

/**
* Validates the entity
*
* @return ConstraintViolationListInterface
*/
public function validate() : ConstraintViolationListInterface
{
$validator = $this->getValidator();

return $validator->validate($this);
}
/**
* The entity validator
*
* @var null|ValidatorInterface
*/
protected $validator;

/**
* Gets the entity validator
*
* @return ValidatorInterface
*/
public function getValidator() : ValidatorInterface
{
if (empty($this->validator)) {
$this->validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();
}

return $this->validator;
}

/**
* Validates the entity
*
* @return ConstraintViolationListInterface
*/
public function validate() : ConstraintViolationListInterface
{
return $this->getValidator()
->validate($this);
}
}
116 changes: 56 additions & 60 deletions src/Http/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,70 +18,66 @@
class App
{

/**
* The application router
*
* @var RouterInterface
*
* @Inject
*/
protected $router;
/**
* The application router
*
* @var RouterInterface
*
* @Inject
*/
protected $router;

/**
* Runs the application
*
* @param ServerRequestInterface $request
*
* @return void
*/
public function run(ServerRequestInterface $request) : void
{
try
{
$response = $this->router->handle($request);
}
catch (MethodNotAllowedException $e)
{
$response = (new ResponseFactory)->createResponse(405)
->withHeader('allow', implode(', ', $e->getAllowedMethods()));
/**
* Runs the application
*
* @param ServerRequestInterface $request
*
* @return void
*/
public function run(ServerRequestInterface $request) : void
{
try {
$response = $this->router->handle($request);
} catch (RouteNotFoundException $e) {
$response = (new ResponseFactory)->createResponse(404);
$response->getBody()->write($response->getReasonPhrase());
} catch (MethodNotAllowedException $e) {
$response = (new ResponseFactory)->createResponse(405)
->withHeader('allow', implode(', ', $e->getAllowedMethods()));
$response->getBody()->write($response->getReasonPhrase());
}

$response->getBody()->write($response->getReasonPhrase());
}
catch (RouteNotFoundException $e)
{
$response = (new ResponseFactory)->createResponse(404);
$this->emit($response);
}

$response->getBody()->write($response->getReasonPhrase());
}
/**
* Emits the given response
*
* @param ResponseInterface $response
*
* @return void
*/
public function emit(ResponseInterface $response) : void
{
$headers = $response->getHeaders();

$this->emit($response);
}
foreach ($headers as $name => $values) {
foreach ($values as $value) {
\header(\sprintf(
'%s: %s',
$name,
$value
), false);
}
}

/**
* Emits the given response
*
* @param ResponseInterface $response
*
* @return void
*/
public function emit(ResponseInterface $response) : void
{
$headers = $response->getHeaders();
\header(\sprintf(
'HTTP/%s %d %s',
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
), true);

foreach ($headers as $name => $values)
{
foreach ($values as $value)
{
\header(\sprintf('%s: %s', $name, $value), false);
}
}

\header(\sprintf('HTTP/%s %d %s',
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
), true);

echo $response->getBody();
}
echo $response->getBody();
}
}
Loading

0 comments on commit 1f84e57

Please sign in to comment.