Maslosoft Sprite API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/sprite
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link http://maslosoft.com/sprite/
*/
namespace Maslosoft\Sprite\Generators;
use Maslosoft\MiniView\MiniView;
use Maslosoft\Sprite\Helpers\FolderChecker;
use Maslosoft\Sprite\Helpers\Namer;
use Maslosoft\Sprite\Interfaces\CssGeneratorInterface;
use Maslosoft\Sprite\Traits\CollectionAwareTrait;
use Maslosoft\Sprite\Traits\ConfigurationAwareTrait;
/**
* CssGenerator
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class CssGenerator implements CssGeneratorInterface
{
use ConfigurationAwareTrait,
CollectionAwareTrait;
/**
* View instance
* @var MiniView
*/
private $mv = null;
public function __construct()
{
$this->mv = new MiniView($this);
}
public function generate()
{
$collection = $this->getCollection();
$config = $this->getConfig();
FolderChecker::check($config);
$css = [];
$squares = [];
$rects = [];
foreach ($collection->getSprites() as $image)
{
if ($image->isSquare())
{
$squares[$image->width] = $image->width;
}
else
{
$size = sprintf('%sx%s', $image->width, $image->height);
$rects[$size] = [
$image->width,
$image->height
];
}
}
// Square icons
foreach ($squares as $size)
{
$params = [
'prefix' => $config->iconCssClass,
'size' => $size
];
$css[] = $this->mv->render('css-square.latte', $params, true);
}
// Rectangle icons
foreach ($rects as $size)
{
$params = [
'prefix' => $config->iconCssClass,
'width' => $size[0],
'height' => $size[1]
];
$css[] = $this->mv->render('css-rect.latte', $params, true);
}
// Generate css for each image
foreach ($collection->getGroups() as $group)
{
$top = $group->height;
foreach ($group->sprites as $image)
{
foreach ($image->packages as $package)
{
$params = [
'cssClass' => Namer::nameCssClass($package, $image),
'horizontal' => -$group->offset,
'vertical' => $top - $group->height
];
$css[] = $this->mv->render('css-icon.latte', $params, true);
}
// NOTE: This must be in sprites loop, not packages loop
$top -= $image->height;
}
}
$filename = sprintf('%s/%s.css', $config->generatedPath, $config->basename);
file_put_contents($filename, implode("\n", $css));
}
}
API documentation generated by ApiGen