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
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Maslosoft\Sprite\Models;
use Maslosoft\Sprite\Helpers\Namer;
use Maslosoft\Sprite\Interfaces\SpritePackageInterface;
use ReflectionClass;
/**
* ConstClass
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class ConstClass
{
/**
* Namespace
* @var string
*/
public $ns = '';
/**
* Short name
* @var string
*/
public $name = '';
/**
* Constants
* @var ConstItem[]
*/
public $constants = [];
/**
* Package
* @var SpritePackageInterface
*/
private $package = null;
/**
* @var string
*/
private $path = '';
public function __construct(SpritePackageInterface $package)
{
$this->package = $package;
$className = $this->package->getConstantsClass();
$path = $this->package->getConstantsClassPath();
if (!empty($path))
{
// Manually create class, as it might not be loaded into opcache
$parts = explode('\\', $className);
$this->name = array_pop($parts);
$this->ns = implode('\\', $parts);
$this->path = sprintf('%s/%s.php', $path, $this->name);
}
else
{
// Use reflection class to get info
$info = new ReflectionClass($className);
$this->ns = $info->getNamespaceName();
$this->name = $info->getShortName();
$this->path = $info->getFileName();
}
}
public function getPath()
{
return $this->path;
}
/**
* Add constant from sprite, but only if in current package.
* @param SpriteImage $sprite
*/
public function add(SpriteImage $sprite)
{
$dest = $this->package->getConstantsClass();
foreach ($sprite->packages as $package)
{
$src = $package->getConstantsClass();
if ($src === $dest)
{
$this->addSprite($sprite);
}
}
}
private function addSprite(SpriteImage $sprite)
{
$item = new ConstItem();
$item->name = Namer::nameConstant($this->package, $sprite);
$item->value = Namer::nameCssClass($this->package, $sprite);
$this->constants[$item->name] = $item;
}
public function __toString()
{
return '<?php';
}
}
API documentation generated by ApiGen