DirectoryGetter.class.php

Go to the documentation of this file.
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     final class DirectoryGetter extends PrototypedGetter
00013     {
00014         public function get($name)
00015         {
00016             if (!isset($this->mapping[$name]))
00017                 throw new WrongArgumentException(
00018                     "knows nothing about property '{$name}'"
00019                 );
00020             
00021             $primitive = $this->mapping[$name];
00022 
00023             $path = $this->object.'/'.$primitive->getName();
00024 
00025             if ($primitive instanceof PrimitiveFile)
00026                 return $path;
00027 
00028             if (!file_exists($path))
00029                 return null;
00030 
00031             if ($primitive instanceof PrimitiveForm) {
00032                 if (!$primitive instanceof PrimitiveFormsList)
00033                     return $path;
00034 
00035                 $result = array();
00036 
00037                 $subDirs = glob($path.'/*');
00038 
00039                 if ($subDirs === false)
00040                     throw new WrongStateException(
00041                         'cannot read directory '.$path
00042                     );
00043 
00044                 foreach ($subDirs as $path)
00045                     $result[basename($path)] = $path;
00046 
00047                 return $result;
00048             }
00049 
00050             for ($i = 0; $i <= 42; ++$i) { // yanetut
00051                 $result = file_get_contents($path);
00052 
00053                 if ($result === false) {
00054                     throw new WrongArgumentException("failed to read $path");
00055                 }
00056 
00057                 if ($result)
00058                     break;
00059 
00060                 // NOTE: empty file COULD mean that data is being prepared now.
00061                 // On heavy loaded systems it means that file was just
00062                 // truncated and we should try again several times.
00063             }
00064 
00065             return $result;
00066         }
00067     }
00068 ?>