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 @@
-
+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));
}