00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2009 by Ivan Y. Khvostishkov * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; either version 3 of the * 00008 * License, or (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00012 abstract class DirectoryBuilder extends PrototypedBuilder 00013 { 00014 protected $directory = null; 00015 protected $permissions = 0700; 00016 protected $identityMap = null; 00017 00018 public function __construct(EntityProto $proto) 00019 { 00020 parent::__construct($proto); 00021 00022 $this->identityMap = new DirectoryContext; 00023 } 00024 00025 public function setDirectory($directory) 00026 { 00027 $this->directory = $directory; 00028 00029 return $this; 00030 } 00031 00032 public function getDirectory() 00033 { 00034 return $this->directory; 00035 } 00036 00037 public function setPermissions($permissions) 00038 { 00039 $this->permissions = $permissions; 00040 00041 return $this; 00042 } 00043 00044 public function getPermissions() 00045 { 00046 return $this->permissions; 00047 } 00048 00049 public function setIdentityMap(DirectoryContext $identityMap) 00050 { 00051 $this->identityMap = $identityMap; 00052 00053 return $this; 00054 } 00055 00056 public function getIdentityMap() 00057 { 00058 return $this->identityMap; 00059 } 00060 00064 public function cloneBuilder(EntityProto $proto) 00065 { 00066 $result = parent::cloneBuilder($proto); 00067 00068 $result-> 00069 setDirectory($this->directory)-> 00070 setPermissions($this->permissions)-> 00071 setIdentityMap($this->identityMap); 00072 00073 return $result; 00074 } 00075 00076 public function cloneInnerBuilder($property) 00077 { 00078 $this->checkDirectory(); 00079 00080 $result = parent::cloneInnerBuilder($property); 00081 00082 $result-> 00083 setDirectory($this->directory.'/'.$property)-> 00084 setPermissions($this->permissions)-> 00085 setIdentityMap($this->identityMap); 00086 00087 return $result; 00088 } 00089 00090 public function makeListItemBuilder($object) 00091 { 00092 $this->checkDirectory(); 00093 00094 if (!$object instanceof Identifiable) 00095 throw new WrongArgumentException( 00096 'cannot build list of items without identity' 00097 ); 00098 00099 return $this->cloneBuilder($this->proto)-> 00100 setPermissions($this->permissions)-> 00101 setDirectory($this->directory.'/'.$object->getId()); 00102 } 00103 00104 protected function createEmpty() 00105 { 00106 $result = $this->directory; 00107 00108 if (!file_exists($result)) 00109 mkdir($result, $this->permissions, true); 00110 elseif (is_link($result)) { 00111 throw new WrongStateException( 00112 'cannot make object by reference: '.$this->directory 00113 ); 00114 } 00115 00116 return $result; 00117 } 00118 00119 protected function safeClean() 00120 { 00121 if (file_exists($this->directory) || is_link($this->directory)) { 00122 if (!is_link($this->directory)) { 00123 throw new WrongStateException( 00124 'you should remove the storage ' 00125 .$this->directory 00126 .' by your hands' 00127 ); 00128 } 00129 00130 unlink($this->directory); 00131 } 00132 00133 return $this; 00134 } 00135 00136 protected function checkDirectory() 00137 { 00138 if (!$this->directory) 00139 throw new WrongStateException( 00140 'you must specify the context for this builder' 00141 ); 00142 00143 return $this; 00144 } 00145 } 00146 ?>