From 0309310e6eac83f5f8bdb0fbf01d2594b350005a Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Wed, 5 Jun 2013 17:29:35 +0200 Subject: [PATCH 1/3] removed doctrine from config --- app/config/config.yml | 19 +------------------ app/config/config_prod.yml | 6 ------ 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/app/config/config.yml b/app/config/config.yml index 79a03410..bf1658d4 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -38,23 +38,6 @@ assetic: #yui_css: # jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar -# Doctrine Configuration -#doctrine: -# dbal: -# driver: %database_driver% -# host: %database_host% -# port: %database_port% -# dbname: %database_name% -# user: %database_user% -# password: %database_password% -# charset: UTF8 -# # if using pdo_sqlite as your database driver, add the path in parameters.yml -# # e.g. database_path: %kernel.root_dir%/data/data.db3 -# # path: %database_path% -# -# orm: -# auto_generate_proxy_classes: %kernel.debug% -# auto_mapping: true pugx_badge: badges: - { name: 'total', label: 'Total Downloads', route: "pugx_badge" } @@ -69,4 +52,4 @@ snc_redis: default: type: predis alias: default - dsn: redis://localhost + dsn: redis://localhost \ No newline at end of file diff --git a/app/config/config_prod.yml b/app/config/config_prod.yml index e268dd06..7f39229e 100644 --- a/app/config/config_prod.yml +++ b/app/config/config_prod.yml @@ -11,12 +11,6 @@ framework: #validation: # cache: apc -#doctrine: -# orm: -# metadata_cache_driver: apc -# result_cache_driver: apc -# query_cache_driver: apc - monolog: handlers: main: From 82f85915a61964d99badc324172776dc2ec6b203 Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Wed, 5 Jun 2013 17:31:21 +0200 Subject: [PATCH 2/3] moved redis hostname to the parameter, fixed bug and created the createErrorImage --- app/badge-assets/images/error.png | Bin 0 -> 726 bytes app/config/config.yml | 2 +- app/config/parameters.yml.dist | 1 + .../Controller/BadgeController.php | 19 ++++++++++---- src/PUGX/BadgeBundle/Service/ImageCreator.php | 24 +++++++++++++++--- .../Tests/Controller/BadgeControllerTest.php | 24 ++++++++++++++++-- 6 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 app/badge-assets/images/error.png diff --git a/app/badge-assets/images/error.png b/app/badge-assets/images/error.png new file mode 100644 index 0000000000000000000000000000000000000000..ffc61c0ee06049ff338099a603efc67a4d02f7f3 GIT binary patch literal 726 zcmV;{0xA88P)}(Ob@k^m^IZNLU-eLnah_q!`pfD-gozn7Kuch zzzon2IQY-;AYF}3a009Xqk1$Nod%v;#q;Wv+b%(<0R{ndrPMPkc`lX0X6V*=)At8b$vJ zcbQPA2}J@UL=m6Q#p4P3`g)1S6STKKB$4>?dt>wFn5}X5iFyUG*d|p~0!R!eEcoDyL*g{y}|8v7tHxv?f)TvR(24A!EQD-HgGr`mzv=B`w0fS z+28+Ouz_Y=CX-=e;se{;+Z-Mq;&eLkcsxicP3aps;aGvO2qw6Oo+ z%zm11qDv{4xoD|c0X7uoRsHz*coYy=g3+`M>mD%VJq5uE@07*qo IM6N<$g89isSpWb4 literal 0 HcmV?d00001 diff --git a/app/config/config.yml b/app/config/config.yml index bf1658d4..dea20806 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -52,4 +52,4 @@ snc_redis: default: type: predis alias: default - dsn: redis://localhost \ No newline at end of file + dsn: redis://%redis_hostname% \ No newline at end of file diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index 8b317c27..fd916df1 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -13,3 +13,4 @@ parameters: locale: en secret: ThisTokenIsNotSoSecretChangeIt + redis_hostname: localhost diff --git a/src/PUGX/BadgeBundle/Controller/BadgeController.php b/src/PUGX/BadgeBundle/Controller/BadgeController.php index f6c5e5f2..49bb3f38 100644 --- a/src/PUGX/BadgeBundle/Controller/BadgeController.php +++ b/src/PUGX/BadgeBundle/Controller/BadgeController.php @@ -10,6 +10,7 @@ namespace PUGX\BadgeBundle\Controller; +use Guzzle\Http\Exception\BadResponseException; use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -26,9 +27,9 @@ */ class BadgeController extends ContainerAware { - CONST ERROR_TEXT_GENERIC = 'ERR 1 '; - CONST ERROR_TEXT_NOT_A_NUMBER = 'ERR 2 '; - CONST ERROR_TEXT_CLIENT_EXCEPTION = 'ERR 3 '; + CONST ERROR_TEXT_GENERIC = 'repository'; + CONST ERROR_TEXT_CLIENT_EXCEPTION = 'packagist'; + CONST ERROR_TEXT_CLIENT_BAD_RESPONSE = 'not found?'; /** * Downloads action. @@ -58,18 +59,24 @@ public function downloadsAction($repository, $type = 'total') $imageCreator = $this->container->get('image_creator'); $outputFilename = sprintf('%s.png', $type); $status = 500; + $image = null; try { $package = $this->container->get('package_manager')->fetchPackage($repository); $text = $this->container->get('package_manager')->getPackageDownloads($package, $type); + $image = $imageCreator->createDownloadsImage($text); $status = 200; + } catch (BadResponseException $e) { + $text = self::ERROR_TEXT_CLIENT_BAD_RESPONSE; } catch (UnexpectedValueException $e) { $text = self::ERROR_TEXT_CLIENT_EXCEPTION; } catch (\Exception $e) { $text = self::ERROR_TEXT_GENERIC; } - $image = $imageCreator->createDownloadsImage($text); + if (null == $image) { + $image = $imageCreator->createErrorImage($text); + } return $this->streamImage($status, $image, $outputFilename); } @@ -115,6 +122,8 @@ public function versionAction($repository, $latest = 'stable') } $status = 200; + } catch (BadResponseException $e) { + $error = self::ERROR_TEXT_CLIENT_BAD_RESPONSE; } catch (UnexpectedValueException $e) { $error = self::ERROR_TEXT_CLIENT_EXCEPTION; } catch (\Exception $e) { @@ -122,7 +131,7 @@ public function versionAction($repository, $latest = 'stable') } if (null == $image) { - $image = $this->container->get('image_creator')->createStableImage($error); + $image = $this->container->get('image_creator')->createErrorImage($error); } return $this->streamImage($status, $image, $outputFilename); diff --git a/src/PUGX/BadgeBundle/Service/ImageCreator.php b/src/PUGX/BadgeBundle/Service/ImageCreator.php index 15842153..ca34647a 100644 --- a/src/PUGX/BadgeBundle/Service/ImageCreator.php +++ b/src/PUGX/BadgeBundle/Service/ImageCreator.php @@ -22,7 +22,7 @@ class ImageCreator implements ImageCreatorInterface { private $logger; protected $dispatcher; - protected $imageNames = array('empty' => 'empty.png', 'downloads' => 'downloads.png', 'stable' => 'stable.png', 'unstable' => 'unstable.png'); + protected $imageNames = array('empty' => 'empty.png', 'downloads' => 'downloads.png', 'stable' => 'stable.png', 'unstable' => 'unstable.png', 'error' => 'error.png'); protected $imagePath; protected $fontPath; protected $defaultFont; @@ -188,7 +188,7 @@ private function createImage($imagePath) } /** - * Create the 'downloads' image with the standard Font and standard Image. + * Create the 'downloads' image with the standard Font and download image template. * * @param string $value * @@ -205,7 +205,7 @@ public function createDownloadsImage($value) } /** - * Create the 'stable' image with the standard Font and standard Image. + * Create the 'stable' image with the standard Font and stable image template. * * @param string $value * @@ -222,7 +222,7 @@ public function createStableImage($value) } /** - * Create the 'stable' image with the standard Font and standard Image. + * Create the 'stable' image with the standard Font and unstable image template. * * @param string $value * @@ -237,4 +237,20 @@ public function createUnstableImage($value = '@dev') return $image; } + + /** + * Create the 'error' image + * + * @param string $value + * + * @return resource + */ + public function createErrorImage($value) + { + $imagePath = $this->imagePath . DIRECTORY_SEPARATOR . $this->imageNames['error']; + $image = $this->createImage($imagePath); + $this->addShadowedText($image, $value, 50, 13.5, 7); + + return $image; + } } diff --git a/src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php b/src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php index ebee5eed..6eafc748 100644 --- a/src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php +++ b/src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php @@ -20,14 +20,20 @@ public function setUp() { // see the config_test.yml, there's different parameters and services. $data = '{"package":{"name":"pugx\/badge-poser","description":"add badges on your readme, such as downloads number or latest version.","time":"2013-05-24T14:45:06+00:00","maintainers":[{"name":"liuggio","email":"liuggio@gmail.com"}],"versions":{"dev-master":{"name":"pugx\/badge-poser","description":"add badges on your readme, such as downloads number or latest version.","keywords":[],"homepage":"","version":"dev-master","version_normalized":"9999999-dev","license":[],"authors":[],"source":{"type":"git","url":"https:\/\/github.com\/PUGX\/badge-poser.git","reference":"024df1d420cd715aea3400bfea9b87ed0f3bb47e"},"dist":{"type":"zip","url":"https:\/\/api.github.com\/repos\/PUGX\/badge-poser\/zipball\/024df1d420cd715aea3400bfea9b87ed0f3bb47e","reference":"024df1d420cd715aea3400bfea9b87ed0f3bb47e","shasum":""},"type":"library","time":"2013-05-24T19:19:25+00:00","autoload":{"psr-0":{"":"src\/"}},"extra":{"symfony-app-dir":"app","symfony-web-dir":"web","branch-alias":{"dev-master":"0.1-dev"}},"require":{"php":">=5.3.3","symfony\/symfony":"2.2.*","doctrine\/orm":">=2.2,<3.0,>=2.2.3","doctrine\/doctrine-bundle":"1.2.*","twig\/extensions":"1.0.*","symfony\/assetic-bundle":"2.1.*","symfony\/swiftmailer-bundle":"2.2.*","symfony\/monolog-bundle":"2.2.*","sensio\/distribution-bundle":"2.2.*","sensio\/framework-extra-bundle":"2.2.*","sensio\/generator-bundle":"2.2.*","jms\/security-extra-bundle":"1.4.*","jms\/di-extra-bundle":"1.3.*","knplabs\/packagist-api":"dev-master"},"require-dev":{"guzzle\/plugin-mock":"*"}},"dev-develop":{"name":"pugx\/badge-poser","description":"add badges on your readme, such as downloads number or latest version.","keywords":[],"homepage":"","version":"dev-develop","version_normalized":"dev-develop","license":[],"authors":[],"source":{"type":"git","url":"https:\/\/github.com\/PUGX\/badge-poser.git","reference":"024df1d420cd715aea3400bfea9b87ed0f3bb47e"},"dist":{"type":"zip","url":"https:\/\/api.github.com\/repos\/PUGX\/badge-poser\/zipball\/024df1d420cd715aea3400bfea9b87ed0f3bb47e","reference":"024df1d420cd715aea3400bfea9b87ed0f3bb47e","shasum":""},"type":"library","time":"2013-05-24T19:19:25+00:00","autoload":{"psr-0":{"":"src\/"}},"extra":{"symfony-app-dir":"app","symfony-web-dir":"web","branch-alias":{"dev-master":"0.1-dev"}},"require":{"php":">=5.3.3","symfony\/symfony":"2.2.*","doctrine\/orm":">=2.2,<3.0,>=2.2.3","doctrine\/doctrine-bundle":"1.2.*","twig\/extensions":"1.0.*","symfony\/assetic-bundle":"2.1.*","symfony\/swiftmailer-bundle":"2.2.*","symfony\/monolog-bundle":"2.2.*","sensio\/distribution-bundle":"2.2.*","sensio\/framework-extra-bundle":"2.2.*","sensio\/generator-bundle":"2.2.*","jms\/security-extra-bundle":"1.4.*","jms\/di-extra-bundle":"1.3.*","knplabs\/packagist-api":"dev-master"},"require-dev":{"guzzle\/plugin-mock":"*"}}},"type":"library","repository":"https:\/\/github.com\/PUGX\/badge-poser","downloads":{"total":99,"monthly":12,"daily":9},"favers":9}}'; - $packagistResponse = new \Guzzle\Http\Message\Response(200); + $this->packagistClient = $this->createPackagistClient($data, 200); + } + + private function createPackagistClient($data, $status = 200) + { + + $packagistResponse = new \Guzzle\Http\Message\Response($status); $packagistResponse->setBody($data); $plugin = new \Guzzle\Plugin\Mock\MockPlugin(); $plugin->addResponse($packagistResponse); $clientHttp = new \Guzzle\Http\Client(); $clientHttp->addSubscriber($plugin); - $this->packagistClient = new Client($clientHttp); + return new Client($clientHttp); } public function testDownloadsAction() @@ -55,4 +61,18 @@ public function testLatestUnstableAction() $this->assertTrue($client->getResponse()->isSuccessful()); } + + public function testPackageDoesntExists() + { + $data = '{"status":"error","message":"Package not found"}'; + + $packagistClient = $this->createPackagistClient($data, 500); + + $client = static::createClient(); + static::$kernel->getContainer()->set('packagist_client', $packagistClient); + $crawler = $client->request('GET', '/pugx/microsoft-lover/d/total.png'); + + $this->assertFalse($client->getResponse()->getContent()); + $this->assertTrue($client->getResponse()->isServerError()); + } } From 04755c3e2975ac78faac5f5523c63dcf3a6414fa Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Wed, 5 Jun 2013 18:50:39 +0200 Subject: [PATCH 3/3] renamed error string, added error to the image filename --- src/PUGX/BadgeBundle/Controller/BadgeController.php | 8 +++++--- .../BadgeBundle/Tests/Controller/BadgeControllerTest.php | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/PUGX/BadgeBundle/Controller/BadgeController.php b/src/PUGX/BadgeBundle/Controller/BadgeController.php index 49bb3f38..d49546f5 100644 --- a/src/PUGX/BadgeBundle/Controller/BadgeController.php +++ b/src/PUGX/BadgeBundle/Controller/BadgeController.php @@ -28,7 +28,7 @@ class BadgeController extends ContainerAware { CONST ERROR_TEXT_GENERIC = 'repository'; - CONST ERROR_TEXT_CLIENT_EXCEPTION = 'packagist'; + CONST ERROR_TEXT_CLIENT_EXCEPTION = 'connection'; CONST ERROR_TEXT_CLIENT_BAD_RESPONSE = 'not found?'; /** @@ -57,7 +57,6 @@ class BadgeController extends ContainerAware public function downloadsAction($repository, $type = 'total') { $imageCreator = $this->container->get('image_creator'); - $outputFilename = sprintf('%s.png', $type); $status = 500; $image = null; @@ -76,8 +75,10 @@ public function downloadsAction($repository, $type = 'total') if (null == $image) { $image = $imageCreator->createErrorImage($text); + $type = 'error'; } + $outputFilename = sprintf('%s.png', $type); return $this->streamImage($status, $image, $outputFilename); } @@ -107,7 +108,6 @@ public function downloadsAction($repository, $type = 'total') public function versionAction($repository, $latest = 'stable') { $image = null; - $outputFilename = sprintf('%s.png', $latest); $error = self::ERROR_TEXT_GENERIC; $status = 500; @@ -132,8 +132,10 @@ public function versionAction($repository, $latest = 'stable') if (null == $image) { $image = $this->container->get('image_creator')->createErrorImage($error); + $latest = 'error'; } + $outputFilename = sprintf('%s.png', $latest); return $this->streamImage($status, $image, $outputFilename); } diff --git a/src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php b/src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php index 6eafc748..df677cb9 100644 --- a/src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php +++ b/src/PUGX/BadgeBundle/Tests/Controller/BadgeControllerTest.php @@ -62,7 +62,7 @@ public function testLatestUnstableAction() $this->assertTrue($client->getResponse()->isSuccessful()); } - public function testPackageDoesntExists() + public function testIfPackageDoesntExist() { $data = '{"status":"error","message":"Package not found"}';