filename = $filename; $this->directory = ltrim(dirname($filename), '.'); $name = basename($this->filename); if(($pos = strpos($name, '.')) !== false) { $this->extension = substr($name, $pos); $name = substr($name, 0, $pos); } else { $this->extension = null; } // Extract version prefix if already applied to this file $pattern = '/^(?.+)' . preg_quote($this->getPrefix()) . '(?[0-9]+)$/'; if(preg_match($pattern, $name, $matches)) { $this->first = $matches['version'] + 1; $this->name = $matches['name']; } else { $this->first = 1; $this->name = $name; } $this->rewind(); } /** * Get numeric prefix * * @return string */ protected function getPrefix() { return Config::inst()->get(__CLASS__, 'version_prefix'); } public function current() { $version = $this->version; // Initially suggest original name if($version === 1) { return $this->filename; } // If there are more than $this->max files we need a new scheme if($version >= $this->max) { $version = substr(md5(time()), 0, 10); } // Build next name $filename = $this->name . $this->getPrefix() . $version . $this->extension; if($this->directory) { $filename = $this->directory . DIRECTORY_SEPARATOR . $filename; } return $filename; } public function key() { return $this->version; } public function next() { $this->version++; } public function rewind() { $this->version = $this->first; } public function valid() { return $this->version <= $this->max; } }