clean code with php-cs-fixer

This commit is contained in:
Manuel Raynaud
2012-11-03 09:39:40 +01:00
parent 67fa4f6706
commit 43ec85bb1e
20 changed files with 273 additions and 322 deletions

View File

@@ -4,15 +4,12 @@ require __DIR__ . '/vendor/symfony/class-loader/Symfony/Component/ClassLoader/Un
require __DIR__ . '/lib/Thelia/Autoload/TheliaUniversalClassLoader.php';
require __DIR__ . '/lib/Thelia/Autoload/TheliaApcUniversalClassLoader.php';
use Thelia\Autoload\TheliaUniversalClassLoader;
use Thelia\Autoload\TheliaApcUniversalClassLoader;
if(extension_loaded('apc') && $env == 'prod'){
if (extension_loaded('apc') && $env == 'prod') {
$loader = new TheliaApcUniversalClassLoader('Thelia');
}
else{
} else {
$loader = new TheliaUniversalClassLoader();
}
@@ -24,8 +21,4 @@ foreach ($namespaces as $namespace => $directory) {
$loader->registerNamespace('Thelia', __DIR__ . '/lib/');
$loader->register();
?>

View File

@@ -1,23 +1,18 @@
<?php
if(!isset($env)){
if (!isset($env)) {
$env = 'prod';
}
//
//use Symfony\Component\DependencyInjection;
//use Symfony\Component\DependencyInjection\Reference;
/**
*
* @file
*
* @file
* Functions needed for Thelia bootstrap
*/
$loader = require __DIR__ . '/autoload.php';
define('THELIA_ROOT', __DIR__ .'/../');
?>

View File

@@ -3,41 +3,42 @@
namespace Thelia\Autoload;
/**
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class TheliaApcUniversalClassLoader extends TheliaUniversalClassLoader {
class TheliaApcUniversalClassLoader extends TheliaUniversalClassLoader
{
private $prefix;
/**
* Constructor
*
*
* Come from Symfony\Component\ClassLoader\ApcUniversalClassLoader
*
* @param string $prefix
*
* @param string $prefix
* @throws \RuntimeException
*/
public function __construct($prefix) {
public function __construct($prefix)
{
if (!extension_loaded('apc')) {
throw new \RuntimeException('Unable to use ApcUniversalClassLoader as APC is not enabled.');
}
$this->prefix = $prefix;
}
/**
* Finds a file by class name while caching lookups to APC.
*
*
* Come from Symfony\Component\ClassLoader\ApcUniversalClassLoader
*
* @param string $class A class name to resolve to file
*
* @return string|null The path, if found
*/
public function findFile($class) {
public function findFile($class)
{
if (false === $file = apc_fetch($this->prefix.$class)) {
apc_store($this->prefix.$class, $file = parent::findFile($class));
}
@@ -45,4 +46,3 @@ class TheliaApcUniversalClassLoader extends TheliaUniversalClassLoader {
return $file;
}
}
?>

View File

@@ -5,78 +5,77 @@ namespace Thelia\Autoload;
use Symfony\Component\ClassLoader\UniversalClassLoader;
/**
* TheliaUniversalClassLoader
*
* TheliaUniversalClassLoader
*
* extends Symfony\Component\ClassLoader\UniversalClassLoader
*
*
* This class respect PSR-0 autoloading standard and allow to load traditionnal Thelia classes
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*
*
*/
class TheliaUniversalClassLoader extends UniversalClassLoader{
class TheliaUniversalClassLoader extends UniversalClassLoader
{
private $directories = array();
/**
*
*
* add path directory where autoload can search files
*
*
* @param string $directory
*/
public function addDirectory($directory){
public function addDirectory($directory)
{
$this->directories[] = $directory;
}
/**
*
*
* add multiple path directory in an array where autoload can search files
*
*
* @param array $directories
*/
public function addDirectories(array $directories){
foreach($directories as $directory){
public function addDirectories(array $directories)
{
foreach ($directories as $directory) {
$this->addDirectory($directory);
}
}
/**
*
*
* return directories where traditional Thelia classes can be found
*
*
* @return array an Array of directories
*/
public function getDirectories(){
public function getDirectories()
{
return $this->directories;
}
/**
*
*
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @param string $class The name of the class
* @return string|null The path, if found
*/
public function findFile($class) {
foreach($this->directories as $directory){
if(is_file($directory.DIRECTORY_SEPARATOR.$class.".class.php")){
public function findFile($class)
{
foreach ($this->directories as $directory) {
if (is_file($directory.DIRECTORY_SEPARATOR.$class.".class.php")) {
return $directory.DIRECTORY_SEPARATOR.$class.".class.php";
}
if(is_file($directory.DIRECTORY_SEPARATOR.$class.".interface.php")){
if (is_file($directory.DIRECTORY_SEPARATOR.$class.".interface.php")) {
return $directory.DIRECTORY_SEPARATOR.$class.".interface.php";
}
}
return parent::findFile($class);
}
}
?>

View File

@@ -6,29 +6,30 @@ use Thelia\Controller\NullControllerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
*
*
* Must be the last controller call. It fixes default values
*
*
* @author Manuel Raynaud <mraynadu@openstudio.fr>
*/
class DefaultController implements NullControllerInterface{
class DefaultController implements NullControllerInterface
{
/**
*
*
* set the default value for thelia
*
*
* In this case there is no action so we have to verify if some needed params are not missing
*
*
* @param \Symfony\Component\HttpFoundation\Request $request
*/
public function noAction(Request $request) {
if($request->query->has('view') === false){
public function noAction(Request $request)
{
if ($request->query->has('view') === false) {
$fond = "index";
if($request->request->has('view')){
if ($request->request->has('view')) {
$fond = $request->request->get('view');
}
$request->query->set('view', $fond);
}
}
}
?>

View File

@@ -5,16 +5,15 @@ namespace Thelia\Controller;
use Symfony\Component\HttpFoundation\Request;
/**
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
interface NullControllerInterface {
interface NullControllerInterface
{
/**
* Nothing to do
*/
public function noAction(Request $request);
}
?>

View File

@@ -7,24 +7,23 @@ use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RequestListener implements EventSubscriberInterface {
class RequestListener implements EventSubscriberInterface
{
protected $container;
public function __construct(ContainerInterface $container) {
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event){
}
public static function getSubscribedEvents(){
public function onKernelRequest(GetResponseEvent $event)
{
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array('onKernelRequest', 30)
);
}
}
?>

View File

@@ -5,63 +5,61 @@ namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Response;
use Thelia\Core\Template\ParserInterface;
/**
*
*
* ViewSubscriber Main class subscribing to view http response.
*
*
* @TODO Look if it's possible to block this definition in dependency-injection
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ViewListener implements EventSubscriberInterface{
class ViewListener implements EventSubscriberInterface
{
private $parser;
/**
*
*
* @param \Thelia\Core\Template\ParserInterface $parser
*/
public function __construct(ParserInterface $parser) {
public function __construct(ParserInterface $parser)
{
$this->parser = $parser;
}
/**
*
*
* Launch the parser defined on the constructor and get the result.
*
*
* The result is transform id needed into a Response object
*
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event){
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$content = $this->parser->getContent();
if($content instanceof Response){
if ($content instanceof Response) {
$event->setResponse($content);
}
else{
} else {
$event->setResponse(new Response($content, $this->parser->getStatus() ?: 200));
}
}
/**
*
*
* Register the method to execute in this class for a specific event (here the kernel.view event)
*
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents(){
public static function getSubscribedEvents()
{
return array(
KernelEvents::VIEW => array('onKernelView'),
);
}
}
?>

View File

@@ -2,82 +2,85 @@
namespace Thelia\Core\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Thelia\Core\Template\ParserInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
/**
*
*
* Master class of Thelia's parser. The loop mechnism depends of this parser
*
*
* From this class all the parser is lunch
*
*
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class Parser implements ParserInterface {
class Parser implements ParserInterface
{
const PREFIXE = 'prx';
const SHOW_TIME = true;
const ALLOW_DEBUG = true;
const USE_CACHE = true;
protected $container;
protected $content;
protected $status = 200;
public function __construct(ContainerBuilder $container) {
public function __construct(ContainerBuilder $container)
{
$this->container = $container;
}
/**
*
*
* This method must return a Symfony\Component\HttpFoudation\Response instance or the content of the response
*
*
*/
public function getContent() {
public function getContent()
{
$this->loadParser();
$this->content = "toto";
return $this->content;
}
/**
*
*
* set $content with the body of the response or the Response object directly
*
*
* @param string|Symfony\Component\HttpFoundation\Response $content
*/
public function setContent($content) {
public function setContent($content)
{
$this->content = $content;
}
/**
*
*
* @return type the status of the response
*/
public function getStatus() {
public function getStatus()
{
return $this->status;
}
/**
*
*
* status HTTP of the response
*
*
* @param int $status
*/
public function setStatus($status) {
public function setStatus($status)
{
$this->status = $status;
}
public function loadParser(){
public function loadParser()
{
}
}
?>

View File

@@ -3,25 +3,23 @@
namespace Thelia\Core\Template;
/**
*
*
*
*
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*
*
*/
interface ParserInterface {
interface ParserInterface
{
/**
*
*
*/
public function getContent();
public function setContent($content);
public function getStatus();
public function setStatus($status);
}
?>

View File

@@ -4,90 +4,88 @@ namespace Thelia\Core;
/**
* Root class of Thelia
*
*
* It extends Symfony\Component\HttpKernel\Kernel for changing some fonctionnality
*
*
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Thelia\Core\TheliaBundle;
class Thelia extends Kernel {
class Thelia extends Kernel
{
/**
* Initializes the service container.
*
*
* @TODO cache container initialization
*
* The cached version of the service container is used when fresh, otherwise the
* container is built.
*/
protected function initializeContainer(){
protected function initializeContainer()
{
$this->container = $this->buildContainer();
$this->container->set('kernel', $this);
}
/**
* Builds the service container.
*
* @return ContainerBuilder The compiled service container
*/
protected function buildContainer(){
protected function buildContainer()
{
$container = $this->getContainerBuilder();
foreach($this->bundles as $bundle){
foreach ($this->bundles as $bundle) {
$bundle->build($container);
}
return $container;
}
/**
* return available bundle
*
*
* Part of Symfony\Component\HttpKernel\KernelInterface
*
*
* @return array An array of bundle instances.
*
*
*/
public function registerBundles() {
public function registerBundles()
{
$bundles = array(
/* TheliaBundle contain all the dependency injection description */
new TheliaBundle()
);
/**
* OTHER CORE BUNDLE CAN BE DECLARE HERE AND INITIALIZE WITH SPECIFIC CONFIGURATION
*
*
* HOW TO DECLARE OTHER BUNDLE ? ETC
*/
return $bundles;
}
/**
* Loads the container configuration
*
*
* part of Symfony\Component\HttpKernel\KernelInterface
*
* @param LoaderInterface $loader A LoaderInterface instance
*
* @api
*/
public function registerContainerConfiguration(LoaderInterface $loader){
public function registerContainerConfiguration(LoaderInterface $loader)
{
//Nothing is load here but it's possible to load container configuration here.
//exemple in sf2 : $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}
}
?>

View File

@@ -10,42 +10,42 @@ use Symfony\Component\DependencyInjection\Scope;
/**
* First Bundle use in Thelia
* It initialize dependency injection container.
*
*
* @TODO load configuration from thelia plugin
* @TODO register database configuration.
*
*
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class TheliaBundle extends Bundle {
class TheliaBundle extends Bundle
{
/**
*
*
* Construct the depency injection builder
*
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function build(ContainerBuilder $container) {
public function build(ContainerBuilder $container)
{
$container->addScope( new Scope('request'));
$container->register('request', 'Symfony\Component\HttpFoundation\Request')
->setSynthetic(true);
$container->register('controller.default','Thelia\Controller\DefaultController');
$container->register('matcher.default','Thelia\Routing\Matcher\DefaultMatcher')
->addArgument(new Reference('controller.default'));
$container->register('matcher','Thelia\Routing\Matcher\theliaMatcherCollection')
->addMethodCall('add', array(new Reference('matcher.default'), -255))
//->addMethodCall('add','a matcher class (instance or class name)
;
$container->register('resolver', 'Symfony\Component\HttpKernel\Controller\ControllerResolver');
$container->register('parser','Thelia\Core\Template\Parser')
->addArgument(new Reference('service_container'))
;
@@ -55,13 +55,13 @@ class TheliaBundle extends Bundle {
$container->register('listener.router', 'Symfony\Component\HttpKernel\EventListener\RouterListener')
->setArguments(array(new Reference('matcher')))
;
/**
* @TODO add an other listener on kernel.request for checking some params Like check if User is log in, set the language and other.
*
*
* $container->register()
*
*
*
*
* $container->register('listener.request', 'Thelia\Core\EventListener\RequestListener')
* ->addArgument(new Reference('');
* ;
@@ -70,26 +70,23 @@ class TheliaBundle extends Bundle {
$container->register('thelia.listener.view','Thelia\Core\EventListener\ViewListener')
->addArgument(new Reference('parser'))
;
$container->register('http_kernel','Symfony\Component\HttpKernel\HttpKernel')
->addArgument(new Reference('dispatcher'))
->addArgument(new Reference('dispatcher'))
->addArgument(new Reference('resolver'))
;
;
$container->register('dispatcher','Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher')
->addArgument(new Reference('service_container'))
->addMethodCall('addSubscriber', array(new Reference('listener.router')))
->addMethodCall('addSubscriber', array(new Reference('thelia.listener.view')))
;
// DEFINE DEFAULT PARAMETER LIKE
/**
* @TODO learn about container compilation
*/
}
}
?>

View File

@@ -8,29 +8,26 @@ use Thelia\Controller\NullControllerInterface;
/**
* Default matcher when no action is needed and there is no result for urlmatcher
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class DefaultMatcher implements RequestMatcherInterface{
class DefaultMatcher implements RequestMatcherInterface
{
protected $controller;
public function __construct(NullControllerInterface $controller) {
public function __construct(NullControllerInterface $controller)
{
$this->controller = $controller;
}
public function matchRequest(Request $request) {
public function matchRequest(Request $request)
{
$objectInformation = new \ReflectionObject($this->controller);
$parameter = array(
'_controller' => $objectInformation->getName().'::noAction'
'_controller' => $objectInformation->getName().'::noAction'
);
return $parameter;
}
}
?>

View File

@@ -10,91 +10,90 @@ use Symfony\Component\Routing\RequestContext;
use Symfony\Component\HttpFoundation\Request;
/**
*
* Collection of Matcher.
*
*
* Collection of Matcher.
*
* Matcher resolve request into controller::method
* exemple of Matcher : UrlMatcher of HttpKernel component (but it implements UrlMatcherInterface and not RequestMatcherInterface.
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class TheliaMatcherCollection implements RequestMatcherInterface, RequestContextAwareInterface {
class TheliaMatcherCollection implements RequestMatcherInterface, RequestContextAwareInterface
{
protected $context;
protected $matchers = array();
protected $defaultMatcher;
protected $sortedMatchers = array();
/**
* Constructor
*
*
* Check if this constructor is needed (is RequestContext needed ? )
*/
public function __construct() {
public function __construct()
{
$this->context = new RequestContext();
}
/**
* allow to add a matcher routing class to the matchers collection
* matcher must implement RequestMatcherInterface
*
*
* priority can be fixed with $priority parameter
*
*
* @param RequestMatcherInterface $matcher
* @param int $priority set the priority of the added matcher
*
* @param int $priority set the priority of the added matcher
*
*/
public function add(RequestMatcherInterface $matcher, $priority = 0){
if(!is_object($matcher)){
public function add(RequestMatcherInterface $matcher, $priority = 0)
{
if (!is_object($matcher)) {
$matcher = new $matcher();
}
if(!isset($this->matchers[$priority])){
if (!isset($this->matchers[$priority])) {
$this->matchers[$priority] = array();
}
$this->matchers[$priority][] = $matcher;
$this->sortedMatchers = array();
}
/**
*
*
* Sort Matchers by priority
*
*
* @return array Array of matchers sorted by priority.
*/
public function getSortedMatchers(){
if(empty($this->sortedMatchers)){
public function getSortedMatchers()
{
if (empty($this->sortedMatchers)) {
$this->sortedMatchers = $this->sortMatchers();
}
return $this->sortedMatchers;
}
/**
*
*
* Sort the matcher by priority
*
*
* @return array Array of matchers sorted by priority.
*/
public function sortMatchers(){
public function sortMatchers()
{
$sortedMatchers = array();
krsort($this->matchers);
foreach($this->matchers as $matcher){
foreach ($this->matchers as $matcher) {
$sortedMatchers = array_merge($sortedMatchers,$matcher);
}
return $sortedMatchers;
}
/**
* Tries to match a request with a set of routes.
*
@@ -108,38 +107,37 @@ class TheliaMatcherCollection implements RequestMatcherInterface, RequestContext
* @throws ResourceNotFoundException If no matching resource could be found
* @throws MethodNotAllowedException If a matching resource was found but the request method is not allowed
*/
public function matchRequest(Request $request) {
if(empty($this->matchers)){
public function matchRequest(Request $request)
{
if (empty($this->matchers)) {
throw new \InvalidArgumentException('there is no matcher added to the TheliaMatcherCollection');
}
foreach($this->getSortedMatchers() as $matcher){
try{
foreach ($this->getSortedMatchers() as $matcher) {
try {
return $matcher->matchRequest($request);
}
catch (ResourceNotFoundException $e){
} catch (ResourceNotFoundException $e) {
//no action, wait for next matcher
}
catch(MethodNotAllowedException $e){
} catch (MethodNotAllowedException $e) {
/**
* @todo what todo with a MethodNotAllowedException ?
*/
}
}
throw new ResourceNotFoundException('No one matcher in this collection matched the current request');
}
/**
* Sets the request context.
*
* @param RequestContext $context The context
*
*/
public function setContext(RequestContext $context){
public function setContext(RequestContext $context)
{
$this->context = $context;
}
/**
@@ -148,8 +146,8 @@ class TheliaMatcherCollection implements RequestMatcherInterface, RequestContext
* @return RequestContext The context
*
*/
public function getContext(){
public function getContext()
{
return $this->context;
}
}
}
?>

View File

@@ -1,4 +1,3 @@
<?php
require __DIR__ . '/core/bootstrap.php';
?>

View File

@@ -15,16 +15,12 @@ $trustIp = array(
$request = Request::createFromGlobals();
if( false === in_array($request->getClientIp(), $trustIp)){
if ( false === in_array($request->getClientIp(), $trustIp)) {
//change request to send to a 404 error page
}
$thelia = new Thelia('dev', true);
$response = $thelia->handle($request)->prepare($request)->send();
$thelia->terminate($request, $response);
?>

View File

@@ -6,7 +6,6 @@
//database type : mysql, sqlite, pgsql, etc
define('THELIA_DB_TYPE','mysql');
// database login
define('THELIA_BD_LOGIN', '__DB_LOGIN__');
@@ -15,6 +14,3 @@ define('THELIA_BD_PASSWORD', '__DB_PASSWORD__');
//database DSN
define('THELIA_DB_DSN','mysql:dbname=__DB_NAME__;host:__DB_HOST__');
?>

View File

@@ -12,8 +12,4 @@ define('THELIA_BD_LOGIN', '__DB_LOGIN__');
// database password
define('THELIA_BD_PASSWORD', '__DB_PASSWORD__');
define('THELIA_DB_DSN','pgsql:dbname=__DB_NAME__;host:__DB_HOST__');
?>

View File

@@ -12,8 +12,4 @@ define('THELIA_BD_LOGIN', '__DB_LOGIN__');
// database password
define('THELIA_BD_PASSWORD', '__DB_PASSWORD__');
define('THELIA_DB_DSN','sqlite:__DB_FILE__');
?>

View File

@@ -1,25 +1,18 @@
<?php
if(is_file(__DIR__ . '/config_db.php')){
if (is_file(__DIR__ . '/config_db.php')) {
require __DIR__ . '/config_db.php';
}
else{
} else {
return false;
}
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
$container = new ContainerBuilder();
$container->register('database','Thelia\\Database\\Connection');
$container->register('http_kernel','Symfony\\Component\\HttpKernel\\HttpKernel');
$container->register('session','Symfony\\Component\\HttpFoundation\\Session\\Session');
return $container;
?>