From 0a98c8d89b55a3e875312593a6f9a24537e49d56 Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Mon, 20 Apr 2015 01:38:17 +0200 Subject: [PATCH 1/3] added flat format --- .../bootstrap/fixtures/license-MIT-blue.svg | 36 +++--- .../fixtures/license-MIT-blue_plastic.svg | 18 +++ .../ui_command_creating_image_file.feature | 7 +- features/ui_command_echo_image.feature | 9 +- spec/PUGX/Poser/PoserSpec.php | 4 +- spec/PUGX/Poser/Render/SvgFlatRenderSpec.php | 69 +++++++++++ src/Render/SvgFlatRender.php | 110 ++++++++++++++++++ src/Render/SvgRender.php | 2 +- src/UI/Command.php | 5 +- 9 files changed, 235 insertions(+), 25 deletions(-) create mode 100644 features/bootstrap/fixtures/license-MIT-blue_plastic.svg create mode 100644 spec/PUGX/Poser/Render/SvgFlatRenderSpec.php create mode 100644 src/Render/SvgFlatRender.php diff --git a/features/bootstrap/fixtures/license-MIT-blue.svg b/features/bootstrap/fixtures/license-MIT-blue.svg index 5d9d856..9dcb6fb 100644 --- a/features/bootstrap/fixtures/license-MIT-blue.svg +++ b/features/bootstrap/fixtures/license-MIT-blue.svg @@ -1,18 +1,20 @@ - - - - - - - - - - - - - license - license - MIT - MIT - + + + + + + + + + + + + + + + license + license + MIT + MIT + \ No newline at end of file diff --git a/features/bootstrap/fixtures/license-MIT-blue_plastic.svg b/features/bootstrap/fixtures/license-MIT-blue_plastic.svg new file mode 100644 index 0000000..5d9d856 --- /dev/null +++ b/features/bootstrap/fixtures/license-MIT-blue_plastic.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + license + license + MIT + MIT + + \ No newline at end of file diff --git a/features/ui_command_creating_image_file.feature b/features/ui_command_creating_image_file.feature index 1b50c3a..e7a385e 100644 --- a/features/ui_command_creating_image_file.feature +++ b/features/ui_command_creating_image_file.feature @@ -4,7 +4,12 @@ Feature: Generation of an image As a visitor I want to use the poser script - Scenario: Create the image running a script + Scenario: Create the image running a script with plastic format + When I run "poser license MIT blue -p /tmp/img.svg -f plastic" + Then it should pass + And the content of "/tmp/img.svg" should be equal to "bootstrap/fixtures/license-MIT-blue_plastic.svg" + + Scenario: Create the image running a script with flat format When I run "poser license MIT blue -p /tmp/img.svg" Then it should pass And the content of "/tmp/img.svg" should be equal to "bootstrap/fixtures/license-MIT-blue.svg" \ No newline at end of file diff --git a/features/ui_command_echo_image.feature b/features/ui_command_echo_image.feature index f27df3b..4a093aa 100644 --- a/features/ui_command_echo_image.feature +++ b/features/ui_command_echo_image.feature @@ -4,7 +4,12 @@ Feature: Generation of an image by echo-ing the content As a visitor I want to use the poser script - Scenario: Echo the image running a script + Scenario: Echo the image running a script with plastic format + When I run "poser license MIT blue -f plastic" + Then it should pass + And the same output should be like the content of "bootstrap/fixtures/license-MIT-blue_plastic.svg" + + Scenario: Echo the image running a script with flat format When I run "poser license MIT blue" Then it should pass - And the same output should be like the content of "bootstrap/fixtures/license-MIT-blue.svg" \ No newline at end of file + And the same output should be like the content of "bootstrap/fixtures/license-MIT-blue.svg" diff --git a/spec/PUGX/Poser/PoserSpec.php b/spec/PUGX/Poser/PoserSpec.php index 7f91da4..cd00d9f 100644 --- a/spec/PUGX/Poser/PoserSpec.php +++ b/spec/PUGX/Poser/PoserSpec.php @@ -4,13 +4,13 @@ use PhpSpec\ObjectBehavior; use Prophecy\Argument; -use PUGX\Poser\Render\SvgRender; +use PUGX\Poser\Render\SvgFlatRender; class PoserSpec extends ObjectBehavior { function let() { - $render = new SvgRender(); + $render = new SvgFlatRender(); $this->beConstructedWith(array($render)); } diff --git a/spec/PUGX/Poser/Render/SvgFlatRenderSpec.php b/spec/PUGX/Poser/Render/SvgFlatRenderSpec.php new file mode 100644 index 0000000..e3cb61c --- /dev/null +++ b/spec/PUGX/Poser/Render/SvgFlatRenderSpec.php @@ -0,0 +1,69 @@ +beConstructedWith($calculator); + } + + function it_should_render_a_svg() + { + $badge = Badge::fromURI('version-stable-97CA00.svg'); + $this->render($badge)->shouldBeAValidSVGImage(); + } + + public function getMatchers() + { + return array( + 'beAValidSVGImage' => function($subject) { + + $regex = '/^$/'; + $matches = array(); + + return preg_match($regex, (string) $subject, $matches, PREG_OFFSET_CAPTURE, 0); + } + ); + } + + function it_should_render_a_license_mit_exactly_like_this_svg() + { + $template = << + + + + + + + + + + + + + + license + license + MIT + MIT + + +EOF; + + $badge = Badge::fromURI('license-MIT-blue.svg'); + $this->render($badge)->__toString()->shouldBeLike($template); + } + + +} \ No newline at end of file diff --git a/src/Render/SvgFlatRender.php b/src/Render/SvgFlatRender.php new file mode 100644 index 0000000..588fee3 --- /dev/null +++ b/src/Render/SvgFlatRender.php @@ -0,0 +1,110 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace PUGX\Poser\Render; + +use PUGX\Poser\Badge; +use PUGX\Poser\Calculator\TextSizeCalculatorInterface; +use PUGX\Poser\Calculator\GDTextSizeCalculator; +use PUGX\Poser\Image; +/** + * Class SvgFlatGenerator + * + * @author Giulio De Donato + */ +class SvgFlatRender implements RenderInterface +{ + const VENDOR_COLOR = '#555'; + private $textSizeCalculator; + + private static $template = << + + + + + + + + + + + + + + {{ vendor }} + {{ vendor }} + {{ value }} + {{ value }} + + +EOF; + + /** + * Constructor. + * + * @param TextSizeCalculatorInterface $textSizeCalculator + */ + public function __construct(TextSizeCalculatorInterface $textSizeCalculator = null) + { + $this->textSizeCalculator = $textSizeCalculator; + + if (null === $this->textSizeCalculator) { + $this->textSizeCalculator = new GDTextSizeCalculator(); + } + } + + /** + * @param Badge $badge + * + * @return mixed + */ + public function render(Badge $badge) + { + $parameters = array(); + + $parameters['vendorWidth'] = $this->stringWidth($badge->getSubject()); + $parameters['valueWidth'] = $this->stringWidth($badge->getStatus()); + $parameters['totalWidth'] = $parameters['valueWidth'] + $parameters['vendorWidth']; + $parameters['vendorColor'] = self::VENDOR_COLOR; + $parameters['valueColor'] = $badge->getHexColor(); + $parameters['vendor'] = $badge->getSubject(); + $parameters['value'] = $badge->getStatus(); + $parameters['vendorStartPosition'] = round($parameters['vendorWidth'] / 2, 1) + 1; + $parameters['valueStartPosition'] = $parameters['vendorWidth'] + round($parameters['valueWidth'] / 2, 1) - 1; + + return $this->renderSvg(self::$template, $parameters, $badge->getFormat()); + } + + /** + * A list of all supported formats. + * + * @return array + */ + public function supportedFormats() + { + return array('flat', 'svg'); + } + + private function stringWidth($text) + { + return $this->textSizeCalculator->calculateWidth($text); + } + + private function renderSvg($render, $parameters, $format) + { + foreach ($parameters as $key => $variable) { + $render = str_replace(sprintf('{{ %s }}', $key), $variable, $render); + } + + return Image::createFromString($render, $format); + } +} diff --git a/src/Render/SvgRender.php b/src/Render/SvgRender.php index bc04a04..7492301 100644 --- a/src/Render/SvgRender.php +++ b/src/Render/SvgRender.php @@ -89,7 +89,7 @@ public function render(Badge $badge) */ public function supportedFormats() { - return array('svg'); + return array('plastic'); } private function stringWidth($text) diff --git a/src/UI/Command.php b/src/UI/Command.php index e2fd60a..8077797 100644 --- a/src/UI/Command.php +++ b/src/UI/Command.php @@ -2,6 +2,7 @@ namespace PUGX\Poser\UI; +use PUGX\Poser\Render\SvgFlatRender; use Symfony\Component\Console\Command\Command as BaseCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -26,8 +27,8 @@ class Command extends BaseCommand private function init() { - $this->poser = new Poser(array('svg' => new SvgRender())); - $this->format = 'svg'; + $this->poser = new Poser(array(new SvgRender(), new SvgFlatRender())); + $this->format = 'flat'; $this->header = self::HEADER; } From f4aa2a247bbfb01f6135118341c1fc31f8bcbc45 Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Mon, 20 Apr 2015 01:51:16 +0200 Subject: [PATCH 2/3] removed GD as test dependency --- spec/PUGX/Poser/Render/SvgFlatRenderSpec.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/PUGX/Poser/Render/SvgFlatRenderSpec.php b/spec/PUGX/Poser/Render/SvgFlatRenderSpec.php index e3cb61c..6bad336 100644 --- a/spec/PUGX/Poser/Render/SvgFlatRenderSpec.php +++ b/spec/PUGX/Poser/Render/SvgFlatRenderSpec.php @@ -11,9 +11,9 @@ class SvgFlatRenderSpec extends ObjectBehavior { - function let() + function let(TextSizeCalculatorInterface $calculator) { - $calculator = new GDTextSizeCalculator(); + $calculator->calculateWidth(Argument::any())->willReturn(20); $this->beConstructedWith($calculator); } @@ -39,18 +39,18 @@ public function getMatchers() function it_should_render_a_license_mit_exactly_like_this_svg() { $template = << + - + - - - + + + license From 577f80c8c614df0c8c4e5267d15c4f4c90795035 Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Mon, 20 Apr 2015 16:02:19 +0200 Subject: [PATCH 3/3] fixed svgFlatRenderer --- src/Image.php | 3 +- src/Render/SvgFlatRender.php | 54 +++++++++++++++++++++++++++++++++++- src/UI/Command.php | 10 +++++-- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/Image.php b/src/Image.php index 517478a..2cb95a8 100644 --- a/src/Image.php +++ b/src/Image.php @@ -43,7 +43,7 @@ private function __construct($content, $format) */ public function __toString() { - return $this->content; + return (string) $this->content; } /** @@ -66,4 +66,5 @@ public function getFormat() { return $this->format; } + } diff --git a/src/Render/SvgFlatRender.php b/src/Render/SvgFlatRender.php index 588fee3..cd031c7 100644 --- a/src/Render/SvgFlatRender.php +++ b/src/Render/SvgFlatRender.php @@ -24,7 +24,7 @@ class SvgFlatRender implements RenderInterface { const VENDOR_COLOR = '#555'; private $textSizeCalculator; - +/* private static $template = << @@ -48,6 +48,58 @@ class SvgFlatRender implements RenderInterface EOF; + +quasi ok +รน private static $template = << + + + + + + + + + + + + + + + + + {{ vendor }} + {{ vendor }} + {{ value }} + {{ value }} + + +EOF; +*/ + +private static $template = << + + + + + + + + + + + + + + {{ vendor }} + {{ vendor }} + {{ value }} + {{ value }} + + +EOF; + /** * Constructor. * diff --git a/src/UI/Command.php b/src/UI/Command.php index 8077797..be66dd6 100644 --- a/src/UI/Command.php +++ b/src/UI/Command.php @@ -96,14 +96,18 @@ protected function execute(InputInterface $input, OutputInterface $output) protected function flushImage(InputInterface $input, OutputInterface $output, $imageContent) { - $output->write($imageContent); + $output->write((string) $imageContent); $this->header = ''; } protected function storeImage(InputInterface $input, OutputInterface $output, $path, $imageContent) { $this->printHeaderOnce($output); - $fp = fopen($path,"x"); // if file already exists warning is raised + try { + $fp = @fopen($path, "x"); + } catch (\Exception $e) { + $fp = false; + } if (false == $fp) { throw new \Exception("Error on creating the file maybe file [$path] already exists?"); @@ -112,7 +116,7 @@ protected function storeImage(InputInterface $input, OutputInterface $output, $p if ($written <1 || $written != strlen($imageContent)) { throw new \Exception('Error on writing to file.'); } - fclose($fp); + @fclose($fp); $output->write(sprintf('Image created at %s', $path)); }