Skip to content

Instantly share code, notes, and snippets.

@etki
Last active August 29, 2015 14:20
Show Gist options
  • Save etki/1d68ef7e8041ebc1186a to your computer and use it in GitHub Desktop.
Save etki/1d68ef7e8041ebc1186a to your computer and use it in GitHub Desktop.
Interfaces
<?php
class Application
{
private $cache;
private $filesystem;
public function __construct(CacheInterface $cache, FilesystemInterface $filesystem)
{
$this->cache = $cache;
$this->filesystem = $filesystem;
}
public function clearCache()
{
foreach ($this->cache->getTemporaryFiles() as $file) {
$this->filesystem->removeFile($file);
}
}
}
<?php
// конечно, в PHP нет никакого system, описывающего ОС, но давайте представим, что есть
$filesystem = $system->getFilesystemApi();
if ($system->getType() === 'THETA') {
$filesystemAdapter = new ThetaFilesystemAdapter($filesystem);
} else {
$filesystemAdapter = new LambdaFilesystemAdapter($filesystem);
}
$application = new Application(new FilesystemCache, $filesystemAdapter);
$application->clearCache();
<?php
interface FielsystemInteface
{
public removeFile($path);
}
<?php
class LambdaFilesystemAdapter implements FielsystemInteface
{
private $filesystem;
// какая-нибудь тупая система, которой ну прям ОБЯЗАТЕЛЬНо нужны транзакции
public function __construct(LambdaFilesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function removeFile($path)
{
$this->filesystem->startAwkwardTransaction();
$this->filesystem->delete($path);
$this->filesystem->commitAwkwardTransaction();
}
}
<?php
class ThetaFilesystemAdapter implements FielsystemInteface
{
private $filesystem;
// система, которая принимает на вход только объекты класса URL
public function __construct(ThetaFilesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function removeFile($path)
{
$url = new URL($path);
$this->filesystem->erase($url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment