diff --git a/app/controllers/general.php b/app/controllers/general.php index bc666ab0c1..0447b305e8 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -450,9 +450,11 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw // Branded banner for previews - $transformation = new Transformation(new Preview($executionResponse['body'])); - if ($type === 'deployment' && $transformation->isValid($executionResponse['headers'])) { - $transformation->transform(); + $transformation = new Transformation(); + $transformation->addAdapter(new Preview()); + $transformation->setInput($executionResponse['body']); + $transformation->setTraits($executionResponse['headers']); + if ($type === 'deployment' && $transformation->transform()) { $executionResponse['body'] = $transformation->getOutput(); foreach ($executionResponse['headers'] as $key => $value) { diff --git a/src/Appwrite/Transformation/Adapter.php b/src/Appwrite/Transformation/Adapter.php index a80803b388..b4b5dce8a8 100644 --- a/src/Appwrite/Transformation/Adapter.php +++ b/src/Appwrite/Transformation/Adapter.php @@ -4,22 +4,29 @@ namespace Appwrite\Transformation; abstract class Adapter { + protected mixed $input; protected mixed $output; - public function __construct(protected mixed $input) + public function __construct() { } + public function setInput(mixed $input): self + { + $this->input = $input; + return $this; + } + + public function getOutput(): mixed + { + return $this->output; + } + /** * @param array $traits */ abstract public function isValid(array $traits): bool; - abstract public function transform(): bool; - - public function getOutput(): mixed - { - return $this->output; - } + abstract public function transform(): void; } diff --git a/src/Appwrite/Transformation/Adapter/Mock.php b/src/Appwrite/Transformation/Adapter/Mock.php new file mode 100644 index 0000000000..13ec1cc88b --- /dev/null +++ b/src/Appwrite/Transformation/Adapter/Mock.php @@ -0,0 +1,26 @@ + $traits Mock traits + */ + public function isValid(array $traits): bool + { + if ($traits['mock'] === true) { + return true; + } + + return false; + } + + public function transform(): void + { + $this->output = $this->input; + $this->output = "Mock: " . $this->output; + } +} diff --git a/src/Appwrite/Transformation/Adapter/Preview.php b/src/Appwrite/Transformation/Adapter/Preview.php index efa7346f87..94f1454593 100644 --- a/src/Appwrite/Transformation/Adapter/Preview.php +++ b/src/Appwrite/Transformation/Adapter/Preview.php @@ -27,7 +27,7 @@ class Preview extends Adapter return false; } - public function transform(): bool + public function transform(): void { $this->output = $this->input; @@ -197,7 +197,5 @@ class Preview extends Adapter EOT; $this->output .= $banner; - - return true; } } diff --git a/src/Appwrite/Transformation/Transformation.php b/src/Appwrite/Transformation/Transformation.php index db158f78fb..aceba47386 100644 --- a/src/Appwrite/Transformation/Transformation.php +++ b/src/Appwrite/Transformation/Transformation.php @@ -4,25 +4,71 @@ namespace Appwrite\Transformation; class Transformation { - public function __construct(protected Adapter $adapter) + /** + * @var array $adapters + */ + protected array $adapters; + + /** + * @var array $traits + */ + protected array $traits; + + protected mixed $input; + protected mixed $output; + + public function __construct(array $adapters = []) { + $this->adapters = $adapters; } /** * @param array $traits */ - public function isValid(array $traits): bool + public function setTraits(array $traits): self { - return $this->adapter->isValid($traits); + $this->traits = $traits; + return $this; } + public function setInput(mixed $input): self + { + $this->input = $input; + return $this; + } + + public function addAdapter(Adapter $adapter): self + { + $this->adapters[] = $adapter; + return $this; + } + + /** + * @param array $traits + */ public function transform(): bool { - return $this->adapter->transform(); + foreach ($this->adapters as $adapter) { + if (!$adapter->isValid($this->traits)) { + return false; + } + } + + $output = $this->input; + + foreach ($this->adapters as $adapter) { + $adapter->setInput($output); + $adapter->transform(); + $output = $adapter->getOutput(); + } + + $this->output = $output; + + return true; } public function getOutput(): mixed { - return $this->adapter->getOutput(); + return $this->output; } } diff --git a/tests/unit/Transformation/TransformationTest.php b/tests/unit/Transformation/TransformationTest.php index 08f40fc5c7..a3169026a3 100644 --- a/tests/unit/Transformation/TransformationTest.php +++ b/tests/unit/Transformation/TransformationTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit\Transformation; +use Appwrite\Transformation\Adapter\Mock; use Appwrite\Transformation\Adapter\Preview; use Appwrite\Transformation\Transformation; use PHPUnit\Framework\TestCase; @@ -10,18 +11,33 @@ class TransformationTest extends TestCase { public function testPreview(): void { - $transformer = new Transformation(new Preview('Hello world')); + $input = "Hello world"; - $this->assertFalse($transformer->isValid([])); - $this->assertFalse($transformer->isValid(['content-type' => 'text/plain'])); - $this->assertFalse($transformer->isValid(['content-type' => 'tExT/HtML'])); - $this->assertTrue($transformer->isValid(['content-type' => 'text/html'])); - $this->assertTrue($transformer->isValid(['content-TYPE' => 'text/html'])); - $this->assertTrue($transformer->isValid(['content-TYPE' => 'text/plain, text/html; charset=utf-8'])); + $transformer = new Transformation([new Preview()]); + $transformer->addAdapter(new Mock()); + $transformer->setInput($input); + $transformer->setTraits([]); + + $this->assertFalse($transformer->transform()); + + $transformer->setTraits(['mock' => true]); + $this->assertFalse($transformer->transform()); + + $transformer->setTraits(['mock' => true, 'content-type' => 'text/plain']); + $this->assertFalse($transformer->transform()); + + $transformer->setTraits(['mock' => true, 'content-type' => 'tExT/HtML']); + $this->assertFalse($transformer->transform()); + + $transformer->setTraits(['mock' => false, 'content-type' => 'text/plain, text/html; charset=utf-8']); + $this->assertFalse($transformer->transform()); + + $transformer->setTraits(['mock' => true, 'content-type' => 'text/plain, text/html; charset=utf-8']); $this->assertTrue($transformer->transform()); $this->assertStringContainsString("Hello world", $transformer->getOutput()); $this->assertStringContainsString("Preview by", $transformer->getOutput()); + $this->assertStringContainsString("Mock:", $transformer->getOutput()); } }