for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace KevinGH\Box\Compactor;
use Closure;
use function Opis\Closure\serialize as opis_serialize;
use function Opis\Closure\unserialize as opis_unserialize;
use Serializable;
final class CompactorProxy implements Compactor, Serializable
{
private $createCompactor;
private $compactor;
public function __construct(Closure $createCompactor)
$this->createCompactor = $createCompactor;
// Instantiate a compactor instead of lazily instantiate it in order to ensure the factory closure is correct
// and that the created object is of the created type. Since compactors instantiation should be fast, this is
// a minimal overhead which is an acceptable trade-off for providing early errors.
$this->getCompactor();
}
/**
* {@inheritdoc}
public function compact(string $file, string $contents): string
return $this->getCompactor()->compact($file, $contents);
public function serialize(): string
return opis_serialize($this->createCompactor);
public function unserialize($serialized): void
$this->createCompactor = opis_unserialize($serialized);
public function getCompactor(): Compactor
if (null === $this->compactor) {
$this->compactor = ($this->createCompactor)();
return $this->compactor;