| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
abstract class sfMessageSource_File extends sfMessageSource |
|---|
| 32 |
{ |
|---|
| 33 |
|
|---|
| 34 |
* Separator between culture name and source. |
|---|
| 35 |
* @var string |
|---|
| 36 |
*/ |
|---|
| 37 |
protected $dataSeparator = '.'; |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
* Constructor. |
|---|
| 41 |
* |
|---|
| 42 |
* @param string $source the directory where the messages are stored. |
|---|
| 43 |
* @see MessageSource::factory(); |
|---|
| 44 |
*/ |
|---|
| 45 |
function __construct($source) |
|---|
| 46 |
{ |
|---|
| 47 |
$this->source = (string) $source; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
* Gets the last modified unix-time for this particular catalogue+variant. |
|---|
| 52 |
* Just use the file modified time. |
|---|
| 53 |
* |
|---|
| 54 |
* @param string $source catalogue+variant |
|---|
| 55 |
* @return int last modified in unix-time format. |
|---|
| 56 |
*/ |
|---|
| 57 |
public function getLastModified($source) |
|---|
| 58 |
{ |
|---|
| 59 |
return is_file($source) ? filemtime($source) : 0; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
* Gets the message file for a specific message catalogue and cultural variant. |
|---|
| 64 |
* |
|---|
| 65 |
* @param string $variant message catalogue |
|---|
| 66 |
* @return string full path to the message file. |
|---|
| 67 |
*/ |
|---|
| 68 |
public function getSource($variant) |
|---|
| 69 |
{ |
|---|
| 70 |
return $this->source.'/'.$variant; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
* Determines if the message file source is valid. |
|---|
| 75 |
* |
|---|
| 76 |
* @param string $source message file |
|---|
| 77 |
* @return boolean true if valid, false otherwise. |
|---|
| 78 |
*/ |
|---|
| 79 |
public function isValidSource($source) |
|---|
| 80 |
{ |
|---|
| 81 |
return is_file($source); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
* Gets all the variants of a particular catalogue. |
|---|
| 86 |
* |
|---|
| 87 |
* @param string $catalogue catalogue name |
|---|
| 88 |
* @return array list of all variants for this catalogue. |
|---|
| 89 |
*/ |
|---|
| 90 |
public function getCatalogueList($catalogue) |
|---|
| 91 |
{ |
|---|
| 92 |
$variants = explode('_', $this->culture); |
|---|
| 93 |
$source = $catalogue.$this->dataExt; |
|---|
| 94 |
|
|---|
| 95 |
$catalogues = array($source); |
|---|
| 96 |
|
|---|
| 97 |
$variant = null; |
|---|
| 98 |
|
|---|
| 99 |
for ($i = 0, $max = count($variants); $i < $max; $i++) |
|---|
| 100 |
{ |
|---|
| 101 |
if (strlen($variants[$i]) > 0) |
|---|
| 102 |
{ |
|---|
| 103 |
$variant .= $variant ? '_'.$variants[$i] : $variants[$i]; |
|---|
| 104 |
$catalogues[] = $catalogue.$this->dataSeparator.$variant.$this->dataExt; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
$byDir = $this->getCatalogueByDir($catalogue); |
|---|
| 109 |
$catalogues = array_merge($byDir, array_reverse($catalogues)); |
|---|
| 110 |
|
|---|
| 111 |
return $catalogues; |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
* Traverses through the directory structure to find the catalogues. |
|---|
| 116 |
* This should only be called by getCatalogueList() |
|---|
| 117 |
* |
|---|
| 118 |
* @param string $catalogue a particular catalogue. |
|---|
| 119 |
* @return array a list of catalogues. |
|---|
| 120 |
* @see getCatalogueList() |
|---|
| 121 |
*/ |
|---|
| 122 |
protected function getCatalogueByDir($catalogue) |
|---|
| 123 |
{ |
|---|
| 124 |
$variants = explode('_', $this->culture); |
|---|
| 125 |
$catalogues = array(); |
|---|
| 126 |
|
|---|
| 127 |
$variant = null; |
|---|
| 128 |
|
|---|
| 129 |
for ($i = 0, $max = count($variants); $i < $max; $i++) |
|---|
| 130 |
{ |
|---|
| 131 |
if (strlen($variants[$i]) > 0) |
|---|
| 132 |
{ |
|---|
| 133 |
$variant .= $variant ? '_'.$variants[$i] : $variants[$i]; |
|---|
| 134 |
$catalogues[] = $variant.'/'.$catalogue.$this->dataExt; |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
return array_reverse($catalogues); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
* Returns a list of catalogue and its culture ID. |
|---|
| 143 |
* E.g. array('messages', 'en_AU') |
|---|
| 144 |
* |
|---|
| 145 |
* @return array list of catalogues |
|---|
| 146 |
* @see getCatalogues() |
|---|
| 147 |
*/ |
|---|
| 148 |
public function catalogues() |
|---|
| 149 |
{ |
|---|
| 150 |
return $this->getCatalogues(); |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
* Returns a list of catalogue and its culture ID. This takes care |
|---|
| 155 |
* of directory structures. |
|---|
| 156 |
* E.g. array('messages', 'en_AU') |
|---|
| 157 |
* |
|---|
| 158 |
* @return array list of catalogues |
|---|
| 159 |
*/ |
|---|
| 160 |
protected function getCatalogues($dir = null, $variant = null) |
|---|
| 161 |
{ |
|---|
| 162 |
$dir = $dir ? $dir : $this->getSource($variant); |
|---|
| 163 |
$files = scandir($dir); |
|---|
| 164 |
|
|---|
| 165 |
$catalogue = array(); |
|---|
| 166 |
|
|---|
| 167 |
foreach ($files as $file) |
|---|
| 168 |
{ |
|---|
| 169 |
if (is_dir($dir.'/'.$file) && preg_match('/^[a-z]{2}(_[A-Z]{2,3})?$/', $file)) |
|---|
| 170 |
{ |
|---|
| 171 |
$catalogue = array_merge($catalogue, $this->getCatalogues($dir.'/'.$file, $file)); |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
$pos = strpos($file, $this->dataExt); |
|---|
| 175 |
if ($pos > 0 && substr($file, -1 * strlen($this->dataExt)) == $this->dataExt) |
|---|
| 176 |
{ |
|---|
| 177 |
$name = substr($file, 0, $pos); |
|---|
| 178 |
$dot = strrpos($name, $this->dataSeparator); |
|---|
| 179 |
$culture = $variant; |
|---|
| 180 |
$cat = $name; |
|---|
| 181 |
if (is_int($dot)) |
|---|
| 182 |
{ |
|---|
| 183 |
$culture = substr($name, $dot + 1,strlen($name)); |
|---|
| 184 |
$cat = substr($name, 0, $dot); |
|---|
| 185 |
} |
|---|
| 186 |
$details[0] = $cat; |
|---|
| 187 |
$details[1] = $culture; |
|---|
| 188 |
|
|---|
| 189 |
$catalogue[] = $details; |
|---|
| 190 |
} |
|---|
| 191 |
} |
|---|
| 192 |
sort($catalogue); |
|---|
| 193 |
|
|---|
| 194 |
return $catalogue; |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
public function getId() |
|---|
| 198 |
{ |
|---|
| 199 |
return md5($this->source); |
|---|
| 200 |
} |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|