Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Created August 14, 2013 08:31
Show Gist options
  • Save swvitaliy/6229019 to your computer and use it in GitHub Desktop.
Save swvitaliy/6229019 to your computer and use it in GitHub Desktop.
<?php
class FatalException extends Exception {}
interface DesignFolderIf {
public function getBasePath();
public function getFiles();
}
interface PrototypeIf extends DesignFolderIf {
public function createFork($uniqueKey);
}
class DesignFolder {
private $basePath = NULL;
private $files = NULL;
public function __construct($base_path, $files) {
$this->basePath = $base_path;
$this->files = $files;
}
public function getBasePath() {
return $this->basePath;
}
public function getFiles() {
return $this->files;
}
public function cloneDirTo($uniqueKey) {
$dirPath = $this->makeDir($uniqueKey);
$this->copyFilesTo($dirPath);
return $dirPath;
}
protected function makeDir($uniqueKey) {
$dirPath = $this->getBasePath() . '/forks/' . $uniqueKey;
if (!mkdir($dirPath, 0x0666)) {
throw new Exception("Directory \"" . $uniqueKey . "\" cannot be created");
}
return $dirPath;
}
protected function copyFilesTo($dirPath) {
foreach ($this->getFiles() as $file) {
$source = $this->getBasePath() . '/' . $file;
if (!copy($source, $dirPath . '/' . $file)) {
/*if (!exec('rm -rf "' . $dirPath . '"')) {
throw new FatalException("Cannot remove directory \"{$dirPath}\" for roll back");
}*/
throw new Exception("Cannot copy file \"{$source}\" to directory \"{$dirPath}\"");
}
}
}
}
class DesignPrototype extends DesignFolder
implements PrototypeIf {
public function createFork($uniqueKey) {
$dirPath = $this->cloneDirTo($uniqueKey);
return new DesignTransaction($dirPath, $this->getFiles());
}
}
class DesignTransaction extends DesignFolder {
private $uniqueKey = NULL;
private function storeRef($uniqueKey) {
}
private function removeRef() {
}
public function createInstance() {
$uniqueKey = time().rand(10000,99999);
try {
$newDirPath = $this->getBasePath() . '-' . $uniqueKey;
$instance = new DesignInstance($this->getBasePath(), $this->getFiles());
$dirPath = $instance->cloneDirTo($newDirPath);
$instance = new DesignInstance($newDirPath, $this->getFiles());
$this->storeRef($uniqueKey, $dirPath);
} catch (Exception $e) {
$this->removeRef($uniqueKey);
throw $e;
}
return $instance;
}
public function commit() {
}
public function rollBack() {
}
}
class DesignInstance extends DesignPrototype {
public function getContent($fileName) {
return file_get_contents($this->getBasePath() . '/' . $fileName);
}
public function setContent($fileName, $text) {
file_put_contents($this->getBasePath() . '/' . $fileName, $text);
return $this;
}
public function replaceFile($originFN, $fileKey) {
if (unlink($this->getBasePath() . $originFN)) {
}
}
public function resizeImage($fileName, $size) {
$source = $this->getBasePath() . '/' . $fileName;
$fn = pathinfo($fileName, PATHINFO_FILENAME);
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$cmd = 'convert "' . $source . '" -resize ' . $size .
' "' . $this->getBasePath() . '/' . $fn . '-' . $size . $ext . '"';
exec($cmd, $output, $ret);
if (intval($ret)) {
throw new Exception("Cannot resize image file \"{$source}\"; "
. "command \"{$cmd}\"; output \"{$output}\"");
}
}
}
class DesignGroup {
public function registerInstance(DesignInstance $instance) {
}
}
// example
if (!empty($_POST)) {
$prototype = new DesignPrototype(realpath(__DIR__ . '/../pretty_box'),
array(
'close.png',
'box.css',
'loading.gif',
'warning.png',
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment