diff --git a/.gitignore b/.gitignore index 9fd0a59..ab126f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ /vendor/ /cache/ -/.buildpath -/.project -/.settings/ /composer.lock +/phpcs.xml diff --git a/composer.json b/composer.json index 29ee608..442f5ab 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,13 @@ }, "minimum-stability": "stable", "autoload": { - "psr-0": { - "Hostnet\\HnDependencyInjectionPlugin": "src/" + "psr-4": { + "Hostnet\\HnDependencyInjectionPlugin\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Hostnet\\HnDependencyInjectionPlugin\\": "test/" } } } diff --git a/src/Hostnet/HnDependencyInjectionPlugin/Actions.php b/src/Actions.php similarity index 100% rename from src/Hostnet/HnDependencyInjectionPlugin/Actions.php rename to src/Actions.php diff --git a/src/Hostnet/HnDependencyInjectionPlugin/ApplicationConfiguration.php b/src/ApplicationConfiguration.php similarity index 87% rename from src/Hostnet/HnDependencyInjectionPlugin/ApplicationConfiguration.php rename to src/ApplicationConfiguration.php index fbe0e09..5244a47 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/ApplicationConfiguration.php +++ b/src/ApplicationConfiguration.php @@ -2,17 +2,11 @@ namespace Hostnet\HnDependencyInjectionPlugin; use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\TerminableInterface; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\DependencyInjection\Dumper\PhpDumper; -use Symfony\Component\Config\ConfigCache as Symfony2ConfigCache; -use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -use Symfony\Component\Config\Loader\LoaderResolver; -use Symfony\Component\Config\FileLocator; -use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Use this class as your superclass for your ApplicationConfiguration @@ -61,8 +55,9 @@ public function getConfigCache() public function handle( Request $request, $type = HttpKernelInterface::MASTER_REQUEST, - $catch = true) - { + $catch = true + ) { + return $this->getKernel()->handle($request, $type, $catch); } @@ -77,8 +72,8 @@ public function getCLIApplication() public function terminate( Request $request, Response $response - ) - { + ) { + return $this->getKernel()->terminate($request, $response); } @@ -96,7 +91,9 @@ private function getKernel() throw new \RuntimeException( sprintf( 'The kernel that was built should have been of CachedKernelInterface, got %s', - get_class($this->kernel))); + get_class($this->kernel) + ) + ); } } return $this->kernel; @@ -122,4 +119,4 @@ public function getContainer() { return $this->getKernel()->getContainer(); } -} \ No newline at end of file +} diff --git a/src/Hostnet/HnDependencyInjectionPlugin/CacheWarmer.php b/src/CacheWarmer.php similarity index 97% rename from src/Hostnet/HnDependencyInjectionPlugin/CacheWarmer.php rename to src/CacheWarmer.php index b45e522..d397e1e 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/CacheWarmer.php +++ b/src/CacheWarmer.php @@ -39,10 +39,10 @@ public function isOptional() /** * @see \Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface::warmUp() */ - public function warmUp($cacheDir) + public function warmUp($cache_dir) { if ($this->configuration && ! \sfContext::hasInstance()) { \sfContext::createInstance($this->configuration); } } -} \ No newline at end of file +} diff --git a/src/Hostnet/HnDependencyInjectionPlugin/CachedKernelInterface.php b/src/CachedKernelInterface.php similarity index 99% rename from src/Hostnet/HnDependencyInjectionPlugin/CachedKernelInterface.php rename to src/CachedKernelInterface.php index d42290d..69937b2 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/CachedKernelInterface.php +++ b/src/CachedKernelInterface.php @@ -15,4 +15,4 @@ interface CachedKernelInterface extends KernelInterface * @return bool Whether the cache is still fresh */ public function isFresh(); -} \ No newline at end of file +} diff --git a/src/Hostnet/HnDependencyInjectionPlugin/Components.php b/src/Components.php similarity index 95% rename from src/Hostnet/HnDependencyInjectionPlugin/Components.php rename to src/Components.php index e3e411c..4735e73 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/Components.php +++ b/src/Components.php @@ -31,7 +31,8 @@ protected function get($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON return $config->getContainer()->get($id, $invalid_behavior); } throw new \DomainException( - 'Your app config should extend ApplicationConfiguration'); + 'Your app config should extend ApplicationConfiguration' + ); } /** * Shortcut to return the Doctrine Registry service. @@ -58,6 +59,7 @@ protected function getParameter($name) return $config->getContainer()->getParameter($name); } throw new \DomainException( - 'Your app config should extend ApplicationConfiguration'); + 'Your app config should extend ApplicationConfiguration' + ); } } diff --git a/src/Hostnet/HnDependencyInjectionPlugin/ConfigCache.php b/src/ConfigCache.php similarity index 93% rename from src/Hostnet/HnDependencyInjectionPlugin/ConfigCache.php rename to src/ConfigCache.php index 6a820bf..d25a1a6 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/ConfigCache.php +++ b/src/ConfigCache.php @@ -12,6 +12,7 @@ class ConfigCache extends \sfConfigCache { + // @codingStandardsIgnoreStart /** * Constructor overridden to enforce stricter typing * @@ -21,6 +22,7 @@ public function __construct(ApplicationConfiguration $configuration) { parent::__construct($configuration); } + // @codingStandardsIgnoreEnd /** * @see sfConfigCache::checkConfig() @@ -42,10 +44,11 @@ private function writeDatabaseCache($config_path) $handler = $this->createDatabaseHandler(); if (! ($handler instanceof HnDatabaseConfigHandler)) { throw new \RuntimeException( - 'Someone created a handler of incorrect type ' . get_class($handler)); + 'Someone created a handler of incorrect type ' . get_class($handler) + ); } $full_path = \sfConfig::get('sf_root_dir') . '/' . $config_path; - $data = $handler->execute(); + $data = $handler->execute(); $cache = $this->getCacheName($config_path); $this->writeCacheFile($config_path, $cache, $data); @@ -63,4 +66,4 @@ protected function createDatabaseHandler() return new HnDatabaseConfigHandler($this->configuration->getContainer()); } } -} \ No newline at end of file +} diff --git a/src/Hostnet/HnDependencyInjectionPlugin/DebugUrlTracker.php b/src/DebugUrlTracker.php similarity index 95% rename from src/Hostnet/HnDependencyInjectionPlugin/DebugUrlTracker.php rename to src/DebugUrlTracker.php index 98575fb..ada790a 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/DebugUrlTracker.php +++ b/src/DebugUrlTracker.php @@ -35,7 +35,7 @@ public function onKernelResponse(FilterResponseEvent $event) && (!$event->getRequest()->isXmlHttpRequest()) // Prevent Ajax from creating another bar ) { $this->url = $response_headers->get('x-debug-token-link'); - $link = json_encode($response_headers->get('x-debug-token-link')); + $link = json_encode($response_headers->get('x-debug-token-link')); echo << (function() { diff --git a/src/Hostnet/HnDependencyInjectionPlugin/DoctrinePager.php b/src/DoctrinePager.php similarity index 96% rename from src/Hostnet/HnDependencyInjectionPlugin/DoctrinePager.php rename to src/DoctrinePager.php index 87f5bf3..eef6797 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/DoctrinePager.php +++ b/src/DoctrinePager.php @@ -54,7 +54,7 @@ private function getAlias(QueryBuilder $builder) public function init() { $has_max_record_limit = ($this->getMaxRecordLimit() !== false); - $max_record_limit = $this->getMaxRecordLimit(); + $max_record_limit = $this->getMaxRecordLimit(); $builder = $this->getNewQueryBuilder(); $builder->setFirstResult(0); @@ -66,7 +66,9 @@ public function init() throw new \RuntimeException( sprintf( 'The indice [0][1] is supposed to have the count in it! %s', - json_encode($result))); + json_encode($result) + ) + ); } $count = $result[0][1]; $this->setNbResults($has_max_record_limit ? min($count, $max_record_limit) : $count); diff --git a/src/Hostnet/HnDependencyInjectionPlugin/HnDatabaseConfigHandler.php b/src/HnDatabaseConfigHandler.php similarity index 88% rename from src/Hostnet/HnDependencyInjectionPlugin/HnDatabaseConfigHandler.php rename to src/HnDatabaseConfigHandler.php index 975fa0b..67e5935 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/HnDatabaseConfigHandler.php +++ b/src/HnDatabaseConfigHandler.php @@ -1,14 +1,9 @@ getName()) { + switch ($driver->getName()) { case 'pdo_mysql': if (extension_loaded('mysql')) { return 'mysql'; - } elseif(extension_loaded('mysqli')) { + } elseif (extension_loaded('mysqli')) { return 'mysqli'; - } else { - throw new \RuntimeException('The mysql and mysqli extension both not loaded, need one of them.'); } + throw new \RuntimeException('The mysql and mysqli extension both not loaded, need one of them.'); case 'pdo_pgsql': if (extension_loaded('pgsql')) { return 'pgsql'; - } else { - throw new \RuntimeException('The pgsql extension is not loaded.'); } + throw new \RuntimeException('The pgsql extension is not loaded.'); return 'pgsql'; case 'pdo_sqlite': if (function_exists('sqlite_open')) { return 'sqlite'; - } else { - throw new \RuntimeException('The sqlite extension is not loaded.'); } + throw new \RuntimeException('The sqlite extension is not loaded.'); default: throw new \DomainException(sprintf('Unknown driver "%s"', $driver->getName())); } diff --git a/src/Hostnet/HnDependencyInjectionPlugin/Symfony1Fallback.php b/src/Symfony1Fallback.php similarity index 88% rename from src/Hostnet/HnDependencyInjectionPlugin/Symfony1Fallback.php rename to src/Symfony1Fallback.php index 73df1a4..95b7b1a 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/Symfony1Fallback.php +++ b/src/Symfony1Fallback.php @@ -95,13 +95,23 @@ public function fallbackToSymfony1() $context = \sfContext::createInstance($configuration); try { $context->dispatch(); - } catch(\sfError404Exception $e) { + } catch (\sfError404Exception $e) { // The page was actually not found in sf1, wrap it up nicely throw new NotFoundHttpException('Unable to match route in symfony1 fallback', $e); - } catch(\sfStopException $e) { + } catch (\sfStopException $e) { + return $this->createSymfony2Response($context); } - $code = 0; + return $this->createSymfony2Response($context); + } + + /** + * @param \sfContext context + * @return Response + */ + private function createSymfony2Response(\sfContext $context) + { + $code = 0; $response = new Response(); if ($context->getResponse() instanceof \sfWebResponse) { @@ -113,14 +123,18 @@ public function fallbackToSymfony1() // properly, because this response is overwriting the sf1 response if the content of the body is less than // 4kb large due to output buffering. if (($code === 302 || $code === 304)) { + // @codingStandardsIgnoreStart $response->setStatusCode($code, Response::$statusTexts[$code]); + // @codingStandardsIgnoreEnd $response->headers->set('Location', $web_response->getHttpHeader('Location')); } } // Symfony1 will usually send headers for us // Check if found response code is a known sf2 response code + // @codingStandardsIgnoreStart if (!isset(Response::$statusTexts[$code])) { + // @codingStandardsIgnoreEnd // Lets keep sf2 busy with an empty response. For some ajax // requests it doesn't give a valid code, but thats why the // 200 status code. diff --git a/src/Hostnet/HnDependencyInjectionPlugin/Symfony1Kernel.php b/src/Symfony1Kernel.php similarity index 92% rename from src/Hostnet/HnDependencyInjectionPlugin/Symfony1Kernel.php rename to src/Symfony1Kernel.php index 8ce5b0b..30cb5ae 100644 --- a/src/Hostnet/HnDependencyInjectionPlugin/Symfony1Kernel.php +++ b/src/Symfony1Kernel.php @@ -1,14 +1,13 @@ configuration = $configuration; + ) + ); + parent::__construct($environment, $debug); + $this->configuration = $configuration; } public function getConfiguration() @@ -114,12 +115,12 @@ protected function getContainerLoader(ContainerInterface $container) public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load(__DIR__ . '/config/services.yml'); - if($this->isDebug()) { + if ($this->isDebug()) { // Also add the debug services $loader->load(__DIR__ . '/config/services_debug.yml'); } - $path = $this->getConfigDir(); + $path = $this->getConfigDir(); $resource = 'config_' . $this->environment . '.yml'; if (! file_exists($path . '/' . $resource)) { $resource = 'config.yml'; @@ -137,4 +138,4 @@ protected function buildContainer() $this->is_fresh = false; return parent::buildContainer(); } -} \ No newline at end of file +} diff --git a/src/Hostnet/HnDependencyInjectionPlugin/config/services.yml b/src/config/services.yml similarity index 100% rename from src/Hostnet/HnDependencyInjectionPlugin/config/services.yml rename to src/config/services.yml diff --git a/src/Hostnet/HnDependencyInjectionPlugin/config/services_debug.yml b/src/config/services_debug.yml similarity index 100% rename from src/Hostnet/HnDependencyInjectionPlugin/config/services_debug.yml rename to src/config/services_debug.yml diff --git a/test/ApplicationConfigurationTest.php b/test/ApplicationConfigurationTest.php index 970dc8e..9b1c538 100644 --- a/test/ApplicationConfigurationTest.php +++ b/test/ApplicationConfigurationTest.php @@ -1,15 +1,15 @@ getConfigCache(); - $this->assertInstanceOf('Hostnet\HnDependencyInjectionPlugin\ConfigCache', $cache); + public function testGetConfigCache() + { + $config = new ApplicationConfiguration('test', true); + $cache = $config->getConfigCache(); + $this->assertInstanceOf('Hostnet\HnDependencyInjectionPlugin\ConfigCache', $cache); - // And only one is made - $this->assertTrue($cache === $config->getConfigCache()); - } -} \ No newline at end of file + // And only one is made + $this->assertTrue($cache === $config->getConfigCache()); + } +} diff --git a/test/ConfigCacheTest.php b/test/ConfigCacheTest.php index c32e0ac..ba5737a 100644 --- a/test/ConfigCacheTest.php +++ b/test/ConfigCacheTest.php @@ -1,49 +1,38 @@ getMockBuilder('Hostnet\HnDependencyInjectionPlugin\ApplicationConfiguration') + ->disableOriginalConstructor() + ->getMock(); - protected function createDatabaseHandler() - { - return $this->handler; - } + $handler = $this->getMockBuilder('Hostnet\HnDependencyInjectionPlugin\HnDatabaseConfigHandler') + ->disableOriginalConstructor() + ->getMock(); - public function writeCacheFile($config, $cache, $data) - { - if(is_array($this->args)) { - throw new Exception('Only once please'); - } - $this->args = func_get_args(); - } -} + $handler->expects($this->once()) + ->method('execute') + ->with() + ->will($this->returnValue('muhaha')); -class ConfigCacheTest extends PHPUnit_Framework_TestCase -{ - public function setUp() - { - sfConfig::set('sf_root_dir', '/meh/the_root'); - sfConfig::set('sf_config_cache_dir', '/meh/the_root/cache'); - } + $configuration = new TestConfigCache($app_config); + $configuration->handler = $handler; - public function testCheckConfig() - { - $app_config = $this->getMockBuilder('Hostnet\HnDependencyInjectionPlugin\ApplicationConfiguration')-> - disableOriginalConstructor()-> - getMock(); - $handler = $this->getMockBuilder('Hostnet\HnDependencyInjectionPlugin\HnDatabaseConfigHandler')->disableOriginalConstructor()->getMock(); - $handler->expects($this->once())->method('execute')->with()-> - will($this->returnValue('muhaha')); + $this->assertEquals( + '/meh/the_root/cache/config_databases.yml.php', + $configuration->checkConfig('config/databases.yml') + ); - $configuration = new TestConfigCache($app_config); - $configuration->handler = $handler; - $this->assertEquals('/meh/the_root/cache/config_databases.yml.php', - $configuration->checkConfig('config/databases.yml')); - $expected = array('config/databases.yml', '/meh/the_root/cache/config_databases.yml.php', 'muhaha'); - $this->assertEquals($expected, $configuration->args); - } -} \ No newline at end of file + $expected = array('config/databases.yml', '/meh/the_root/cache/config_databases.yml.php', 'muhaha'); + $this->assertEquals($expected, $configuration->args); + } +} diff --git a/test/ProjectConfiguration.php b/test/ProjectConfiguration.php new file mode 100644 index 0000000..40840fc --- /dev/null +++ b/test/ProjectConfiguration.php @@ -0,0 +1,7 @@ +expects($this->never()) ->method('fallbackToSymfony1'); - $event1 = $this->buildControllerEvent(function () {}); + $event1 = $this->buildControllerEvent(function () { + }); $this->fallback->onKernelController($event1); $event2 = $this->buildResponseEvent(new NotFoundHttpException('henk')); diff --git a/test/TestConfigCache.php b/test/TestConfigCache.php new file mode 100644 index 0000000..9b62137 --- /dev/null +++ b/test/TestConfigCache.php @@ -0,0 +1,22 @@ +handler; + } + + public function writeCacheFile($config, $cache, $data) + { + if (is_array($this->args)) { + throw new Exception('Only once please'); + } + $this->args = func_get_args(); + } +} diff --git a/test/bootstrap.php b/test/bootstrap.php index f442826..9c6925c 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -3,7 +3,4 @@ require_once __DIR__ . '/../vendor/symfony/symfony1/lib/autoload/sfCoreAutoload.class.php'; \sfCoreAutoload::register(); -class ProjectConfiguration extends sfProjectConfiguration -{ - -} \ No newline at end of file +require_once __DIR__ . '/ProjectConfiguration.php';