Skip to content

Commit

Permalink
Code: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 2, 2024
1 parent 7440506 commit 01d10da
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 141 deletions.
18 changes: 6 additions & 12 deletions src/DI/ImageStorageExtension.php
Expand Up @@ -3,9 +3,9 @@
namespace Contributte\ImageStorage\DI;

use Contributte\ImageStorage\ImageStorage;
use Nette;
use Latte;
use Contributte\ImageStorage\Latte\LatteExtension;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\FactoryDefinition;
use Nette\Schema\Expect;
use Nette\Schema\Schema;

Expand All @@ -30,28 +30,22 @@ public function getConfigSchema(): Schema
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$this->config->orig_path = $this->config->orig_path ?? $this->config->data_path;
$this->config->orig_path ??= $this->config->data_path;
$config = (array) $this->config;
$builder->addDefinition($this->prefix('storage'))
->setType(ImageStorage::class)
->setFactory(ImageStorage::class)
->setArguments($config);
}


public function beforeCompile(): void
{
$builder = $this->getContainerBuilder();

/** @var Nette\DI\Definitions\FactoryDefinition $latteFactory */
$latteFactory = $builder->getDefinition('latte.latteFactory');
assert($latteFactory instanceof Nette\DI\Definitions\FactoryDefinition);
if (version_compare(Latte\Engine::VERSION, '3', '<')) {
$latteFactory->getResultDefinition()
->addSetup('Contributte\ImageStorage\Macros\Macros::install(?->getCompiler())', ['@self']);
} else {
$latteFactory->getResultDefinition()->addSetup('addExtension', [new \Contributte\ImageStorage\Latte\LatteExtension]);
}
assert($latteFactory instanceof FactoryDefinition);

$latteFactory->getResultDefinition()->addSetup('addExtension', [new LatteExtension()]);
}

}
31 changes: 12 additions & 19 deletions src/Image.php
Expand Up @@ -10,26 +10,19 @@ class Image

use SmartObject;

/** @var string */
public $data_dir;
public string $data_dir;

/** @var string */
public $data_path;
public string $data_path;

/** @var string */
public $identifier;
public string $identifier;

/** @var string */
public $sha;
public string $sha;

/** @var string */
public $name;
public string $name;

/** @var ImageNameScript|null */
private $script;
private ?ImageNameScript $script = null;

/** @var bool */
private $friendly_url = false;
private bool $friendly_url = false;

/**
* @param bool[]|string[]|ImageNameScript[]|null[] $props
Expand Down Expand Up @@ -59,11 +52,6 @@ public function getPath(): string
return implode('/', [$this->data_path, $this->identifier]);
}

public function __toString(): string
{
return $this->identifier;
}

public function getQuery(): string
{
if ($this->script === null) {
Expand Down Expand Up @@ -91,4 +79,9 @@ public function getScript(): ImageNameScript
return $this->script ?: ImageNameScript::fromIdentifier($this->identifier);
}

public function __toString(): string
{
return $this->identifier;
}

}
49 changes: 17 additions & 32 deletions src/ImageNameScript.php
Expand Up @@ -12,71 +12,62 @@ class ImageNameScript
public const PATTERN = '/__file__(\.(\d+)x(\d+)(crop(\d+)x(\d+)x(\d+)x(\d+))?\.(\w+))?(\.q(\d+))?\.([^\.]+)$/';

/** @var string **/
public $identifier;
public string $identifier;

/** @var string **/
public $original;
public string $original;

/** @var string */
public $namespace;
public string $namespace;

/** @var string */
public $prefix;
public string $prefix;

/** @var string */
public $name;
public string $name;

/** @var int[] **/
public $size = [];
public array $size = [];

/** @var string */
public $flag;
public string $flag;

/** @var int */
public $quality;
public int $quality;

/** @var string */
public $extension;
public string $extension;

/** @var int[] */
public $crop = [];
public array $crop = [];

public function __construct(string $identifier)
{
$this->identifier = $identifier;
}


public static function fromIdentifier(string $identifier): ImageNameScript
{
return self::fromName($identifier);
}


public static function fromName(string $name): ImageNameScript
{
$pattern = preg_replace('/__file__/', '(.*)\/([^\/]{2})\/(.*?)', self::PATTERN);
preg_match($pattern, $name, $matches);

$script = new self($matches[0]);

$script->original = $matches[1] . '/' . $matches[2] . '/' . $matches[3] . '.' . $matches[15];
$script->original = $matches[1] . '/' . $matches[2] . '/' . $matches[3] . '.' . $matches[15];
$script->namespace = $matches[1];
$script->prefix = $matches[2];
$script->name = $matches[3];
$script->size = [(int) $matches[5], (int) $matches[6]];
$script->flag = $matches[12];
$script->quality = $matches[14];
$script->prefix = $matches[2];
$script->name = $matches[3];
$script->size = [(int) $matches[5], (int) $matches[6]];
$script->flag = $matches[12];
$script->quality = $matches[14];
$script->extension = $matches[15];

if ($matches[8] && $matches[9] && $matches[10] && $matches[11]) {
$script->crop = [(int) $matches[8], (int) $matches[9], (int) $matches[10], (int) $matches[11]];
$script->crop = [(int) $matches[8], (int) $matches[9], (int) $matches[10], (int) $matches[11]];
}

return $script;
}


/**
* @param int[] $size
*/
Expand All @@ -85,7 +76,6 @@ public function setSize(array $size): void
$this->size = $size;
}


/**
* @param int[] $crop
*/
Expand All @@ -94,19 +84,16 @@ public function setCrop(array $crop): void
$this->crop = $crop;
}


public function setFlag(string $flag): void
{
$this->flag = $flag;
}


public function setQuality(int $quality): void
{
$this->quality = $quality;
}


public function getIdentifier(): string
{
$identifier = implode('/', [$this->namespace, $this->prefix, $this->name]);
Expand All @@ -128,13 +115,11 @@ public function getIdentifier(): string
return $identifier . '.' . $this->extension;
}


public function hasCrop(): bool
{
return count($this->crop) > 0;
}


public function toQuery(): string
{
if ($this->size && $this->size[0] && $this->size[1]) {
Expand Down

0 comments on commit 01d10da

Please sign in to comment.