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..6bad336 --- /dev/null +++ b/spec/PUGX/Poser/Render/SvgFlatRenderSpec.php @@ -0,0 +1,69 @@ +calculateWidth(Argument::any())->willReturn(20); + $this->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/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 new file mode 100644 index 0000000..cd031c7 --- /dev/null +++ b/src/Render/SvgFlatRender.php @@ -0,0 +1,162 @@ + + * + * 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; + + +quasi ok +รน private static $template = << + + + + + + + + + + + + + + + + + {{ vendor }} + {{ vendor }} + {{ value }} + {{ value }} + + +EOF; +*/ + +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..be66dd6 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; } @@ -95,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?"); @@ -111,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)); }