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
<?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\Interfaces\CollectionAwareInterface;
use Maslosoft\Sprite\Interfaces\ConfigurationAwareInterface;
use Maslosoft\Sprite\Interfaces\GeneratorInterface;
use Maslosoft\Sprite\Models\CheatSheetSprite;
use Maslosoft\Sprite\Traits\CollectionAwareTrait;
use Maslosoft\Sprite\Traits\ConfigurationAwareTrait;
/**
* Generate HTML cheat sheet with all of the icons
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class CheatSheetGenerator implements GeneratorInterface, CollectionAwareInterface, ConfigurationAwareInterface
{
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);
$hasConsts = false;
$sprites = [];
foreach ($collection->getSprites() as $sprite)
{
$cSprite = new CheatSheetSprite($sprite);
$sprites[] = $cSprite;
if (!$hasConsts && $cSprite->hasConstants)
{
$hasConsts = true;
}
}
uasort($sprites, [$this, 'compare']);
$table = $this->mv->render('cheat-sheet.latte', ['sprites' => $sprites, 'hasConsts' => $hasConsts], true);
// Generate raw table
$filename = sprintf('%s/%s-table.html', $config->generatedPath, $config->basename);
file_put_contents($filename, $table);
$cssFileName = sprintf('%s/%s.css', $config->generatedPath, $config->basename);
$pngFileName = sprintf('%s/%s.png', $config->generatedPath, $config->basename);
$pngImage = base64_encode(file_get_contents($pngFileName));
$pattern = sprintf('url(%s.png)', $config->basename);
$replace = sprintf('url(data:image/png;base64,%s)', $pngImage);
$cssContents = file_get_contents($cssFileName);
$css = str_replace($pattern, $replace, $cssContents);
// Generate index
$params = [
'table' => $table,
// Use base name, as cheet sheet is in same directory
// and base name in config might point to sub-folder
'css' => $css,
'hasConsts' => $hasConsts
];
$index = $this->mv->render('cheat-sheet-index.latte', $params, true);
$indexFilename = sprintf('%s/%s-index.html', $config->generatedPath, $config->basename);
file_put_contents($indexFilename, $index);
}
public function compare(CheatSheetSprite $a, CheatSheetSprite $b)
{
if ($a->cssClass == $b->cssClass)
{
return 0;
}
return ($a->cssClass < $b->cssClass) ? -1 : 1;
}
}
API documentation generated by ApiGen