freenet-router |
Subversion Repositories: |
Compare with Previous - Blame - Download
<?php
namespace Phem\ClassLoading;
/**
* @author kubapet
*/
class VersionedClassLoader
{
static function getClass($name, \DateTime $date,$suffix = ".php")
{
$baseDir = str_replace("\\", DS, $name);
$nameArray = explode("\\", $name);
$nameArray[0] = APP_DIR;
$viewName = \Phem\ToolSuite::getLastArrayItem($nameArray);
$lookFor = "";
for($i=0;$i<count($nameArray)-1;$i++)
{
$lookFor .= $nameArray[$i].DS;
}
$lookFor .= $viewName;
$foundFiles = new \Phem\Core\Collection();
foreach(glob($lookFor."*") as $file)
{
$dateStr = \Phem\ToolSuite::getStringBetween($file, $lookFor."_", $suffix);
if ($dateStr=="")
{
$dateStr="1970-01-01";
}
$validFrom = new \DateTime($dateStr);
if ($date >= $validFrom)
{
$foundFiles->set($file,$validFrom);
}
}
if ($foundFiles->count() == 0)
{
//Throw exception
}
$foundFiles->sort(\Phem\Core\CollectionSortType::DESC,"date");
$fileNameArr = explode(DS,$foundFiles->getFirstKey());
end($fileNameArr);
$index = key($fileNameArr);
$file = substr($fileNameArr[$index],0,strlen($fileNameArr[$index])-strlen($suffix));
$nameArray[0] = APP_NAMESPACE;
end($nameArray);
$index = key($nameArray);
$nameArray[$index] = $file;
$className = implode("\\",$nameArray);
return $className;
}
}