Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00020 class MultiPrefixPhpViewResolver implements ViewResolver
00021 {
00022 private $prefixes = array();
00023 private $lastAlias = null;
00024
00025 private $disabled = array();
00026
00027 private $postfix = EXT_TPL;
00028 private $viewClassName = 'SimplePhpView';
00029
00033 public static function create()
00034 {
00035 return new self;
00036 }
00037
00041 public function addFirstPrefix($prefix)
00042 {
00043 array_unshift($this->prefixes, $prefix);
00044
00045 return $this;
00046 }
00047
00051 public function addPrefix($prefix, $alias = null)
00052 {
00053 if (!$alias)
00054 $alias = $this->getAutoAlias($prefix);
00055
00056 Assert::isFalse(
00057 isset($this->prefixes[$alias]),
00058 'alias already exists'
00059 );
00060
00061 $this->prefixes[$alias] = $prefix;
00062
00063 $this->lastAlias = $alias;
00064
00065 return $this;
00066 }
00067
00068 public function getPrefixes()
00069 {
00070 return $this->prefixes;
00071 }
00072
00076 public function dropPrefixes()
00077 {
00078 $this->prefixes = array();
00079 return $this;
00080 }
00081
00082 public function isPrefixDisabled($alias)
00083 {
00084 Assert::isIndexExists(
00085 $this->prefixes,
00086 $alias,
00087 'no such alias: '.$alias
00088 );
00089
00090 return !empty($this->disabled[$alias]);
00091 }
00092
00096 public function disablePrefix($alias = null, $disabled = true)
00097 {
00098 if (!$alias)
00099 $alias = $this->lastAlias;
00100
00101 Assert::isNotNull($alias, 'nothing to disable');
00102 Assert::isIndexExists(
00103 $this->prefixes,
00104 $alias,
00105 'no such alias: '.$alias
00106 );
00107
00108 $this->disabled[$alias] = $disabled;
00109
00110 return $this;
00111 }
00112
00113 public function enablePrefix($alias)
00114 {
00115 return $this->disablePrefix($alias, false);
00116 }
00117
00118 public function getPostfix()
00119 {
00120 return $this->postfix;
00121 }
00122
00126 public function setPostfix($postfix)
00127 {
00128 $this->postfix = $postfix;
00129 return $this;
00130 }
00131
00135 public function resolveViewName($viewName)
00136 {
00137 Assert::isFalse(
00138 ($this->prefixes === array()),
00139 'specify at least one prefix'
00140 );
00141
00142 if ($prefix = $this->findPrefix($viewName))
00143 return $this->makeView($prefix, $viewName);
00144
00145 if (!$this->findPrefix($viewName, false))
00146 throw new WrongArgumentException(
00147 'can not resolve view: '.$viewName
00148 );
00149
00150 return EmptyView::create();
00151 }
00152
00153 public function viewExists($viewName)
00154 {
00155 return ($this->findPrefix($viewName) !== null);
00156 }
00157
00161 public function setViewClassName($viewClassName)
00162 {
00163 $this->viewClassName = $viewClassName;
00164
00165 return $this;
00166 }
00167
00168 public function getViewClassName()
00169 {
00170 return $this->viewClassName;
00171 }
00172
00173 protected function findPrefix($viewName, $checkDisabled = true)
00174 {
00175 foreach ($this->prefixes as $alias => $prefix) {
00176 if (
00177 $checkDisabled
00178 && isset($this->disabled[$alias])
00179 && $this->disabled[$alias]
00180 )
00181 continue;
00182
00183 if (file_exists($prefix.$viewName.$this->postfix))
00184 return $prefix;
00185 }
00186
00187 return null;
00188 }
00189
00193 protected function makeView($prefix, $viewName)
00194 {
00195 return new $this->viewClassName(
00196 $prefix.$viewName.$this->postfix,
00197 $this
00198 );
00199 }
00200
00201 private function getAutoAlias($prefix)
00202 {
00203 return md5($prefix);
00204 }
00205 }
00206 ?>