Commit c2d48cd8 authored by dopry's avatar dopry

Refactored back into the Resource namespace..

implement a  StreamWrapper interface and ResourceStreamWrapper interface.
continued porting file into Resource class.
added ResourceFactory.
Sketched out some hair brained idead about relationships that will probably go no where...
with luck this weekend I may finalize interfaces for Resource and ResourceFactory.
I need to chat with Crell about Factory patterns he likes.
parent 2d4d8f00
......@@ -3,9 +3,17 @@
/**
* private:// stream wrapper class.
*/
class DrupalStreamWrapperPrivate extends DrupalStreamWrapperPublic {
class ResourcePrivateStreamWrapper extends ResourcePublicStreamWrapper {
private $pathKey = 'stream_private_path';
private $pathDefault = 'sites/default/files-private';
/**
* Return the HTML Url of a private file.
*/
function htmlUrl($url) {
$basepath = variable_get($this->pathKey, $this->pathDefault);
return url('system/files/' . parse_url($url, PHP_URL_PATH), array('absolute' => TRUE));
}
}
......@@ -2,16 +2,27 @@
/**
* public:// stream wrapper class.
*
* This class provides support for storing publicly
* accessible files with the Drupal resource api.
*/
class DrupalStreamWrapperPublic extends DrupalStreamWrapper {
class ResourcePublicStreamWrapper extends ResourceStreamWrapper {
// A handle to the file opened by stream_open().
private $pathKey = 'stream_public_path';
private $pathDefault = 'sites/default/files';
/**
* interpolate the url for a public stream into a
* real stream or path.
* Return the HTML Url of a public file.
*/
function htmlUrl($url) {
$basepath = variable_get($this->pathKey, $this->pathDefault);
$path = parse_url($url, PHP_URL_PATH);
return $GLOBALS['base_url'] . '/' . $basepath . '/' . str_replace('\\', '/', $path);
}
/**
* Interpolate the url path, adding the public files path.
*/
function interpolateUrl($url) {
$basepath = variable_get($this->pathKey, $this->pathDefault);
......@@ -22,12 +33,19 @@ class DrupalStreamWrapperPublic extends DrupalStreamWrapper {
$path = $basepath . parse_url($url, PHP_URL_PATH);;
}
else {
// interpolate relative paths for basepath, and strip relative paths from
// url path.
// interpolate relative paths for basepath, and strip relative paths from
// url path.
$path = realpath($basepath) . str_replace('/..','', parse_url($url, PHP_URL_PATH));
}
return $path;
}
/**
* Return the mime type of a file.
*/
function mime($url) {
return file_get_mimetype(basename($url));
}
}
<?php
/**
* An asbtract class for custom DrupalStreamWrappers to extend.
* Definition for a Resource stream wrapper in Drupal.
*/
interface ResourceStreamWrapperInterface extends StreamWrapperInterface {
/**
* Return the html accessible URL for a resource.
* @param $url
* Stream wrapper resource url.
* @return string
*/
function htmlUrl($url);
/**
* Return the mime type of a resource.
* @param $url
* Stream wrapper resource url.
* @return string
*/
function mime($url);
/**
* Return an absolute stream resource URL.
* @param $url
* Stream wrapper resource url.
* @return string
*/
function interpolateUrl($url);
}
/**
* Generic PHP stream wrapper interface.
* @see: http://php.net/manual/en/function.stream-wrapper-register.php
*/
interface StreamWrapperInterface {
public function stream_open($url, $mode, $options, &$opened_url);
public function stream_close();
// Undocumented @see: http://us.php.net/flock
public function stream_lock($operation);
public function stream_read($count);
public function stream_write($data);
public function stream_eof();
public function stream_seek($offset, $whence);
public function stream_flush();
public function stream_tell();
public function stream_stat();
public function unlink($url) ;
public function rename($fromUrl, $toUrl);
public function mkdir($url, $mode, $options);
public function rmdir($url, $options);
public function url_stat($url, $flags);
public function dir_opendir($url, $options);
public function dir_readdir() ;
public function dir_rewinddir();
public function dir_closedir() ;
}
/**
* A base class for Resource Stream Wrappers.
*
* This class provides a complete stream wrapper implementation. It passes
* incoming URL's through an interpolation method then recursively calls
......@@ -11,12 +69,19 @@
* method to rewrite the URL before is it passed back into the calling function.
*
*/
class DrupalStreamWrapper {
private $handle;
abstract class ResourceStreamWrapper implements ResourceStreamWrapperInterface {
private $handle = NULL;
// parse the generic part of the URL following scheme.
function interpolateUrl($url) {
return '';
return $url;
}
function htmlUrl($url) {
return $url;
}
function mime($url) {
return 'application/octet-stream';
}
/**
......
......@@ -5,7 +5,7 @@
* user defined stream wrappers in PHP.
*/
class DrupalResourceStreamWrapperManager {
class ResourceStreamWrapperManager {
// stream wrapper registry
private $wrappers = array();
......@@ -19,7 +19,7 @@ class DrupalResourceStreamWrapperManager {
public static function singleton() {
static $instance = NULL;
if (is_null($instance)) {
$instance = new DrupalResourceStreamWrapperManager();
$instance = new ResourceStreamWrapperManager();
}
return $instance;
}
......@@ -31,11 +31,9 @@ class DrupalResourceStreamWrapperManager {
* @return bool result of stream_wrapper_register
* @see: http://us3.php.net/manual/en/function.stream-wrapper-register.php
*/
function register($scheme, $DrupalStreamWrapperClass, $DrupalResourceClass) {
//resource_debug("StreamWrapperManager:: register($scheme, $DrupalStreamWrapperClass, $DrupalResourceClass)");
$this->wrappers[$scheme]['DrupalStreamWrapperClass'] = $DrupalStreamWrapperClass;
$this->wrappers[$scheme]['DrupalResourceClass'] = $DrupalResourceClass;
return stream_wrapper_register($scheme, $DrupalStreamWrapperClass);
function register($scheme, $classname) {
$this->wrappers[$scheme] = $classname;
return stream_wrapper_register($scheme, $classname);
}
function unregister($scheme) {
......@@ -51,8 +49,8 @@ class DrupalResourceStreamWrapperManager {
* @param string $scheme stream scheme.
* @return mixed string is a scheme has a registered handler or FALSE.
*/
function streamWrapperClass($scheme) {
if (empty(self::$this->wrappers[$scheme]['DrupalStreamWrapperClass'])) {
function classname($scheme) {
if (empty(self::$this->wrappers[$scheme])) {
return FALSE;
}
return self::$this->wrappers[$scheme];
......@@ -63,14 +61,10 @@ class DrupalResourceStreamWrapperManager {
* @param string $scheme stream scheme.
* @return mixed string is a scheme has a registered handler or FALSE.
*/
function resourceClass($scheme) {
if (empty(self::$this->wrappers[$scheme]['DrupalResourceClass'])) {
return FALSE;
}
return self::$this->wrappers[$scheme];
function scheme($class) {
return array_search(self::$this->wrappers, $class);
}
/**
* Return the DrupalStreamWrapperManager's wrapper registry.
*/
......
......@@ -4,7 +4,7 @@ description = A stream wrapper enabled replacement for core filehandling.
package = Media
core = 7.x
files[] = resource.module
files[] = DrupalResourceStreamWrapperManager.class.php
files[] = DrupalStreamWrapper.class.php
files[] = DrupalStreamWrapperPublic.class.php
files[] = DrupalStreamWrapperPrivate.class.php
files[] = ResourceStreamWrapperManager.inc
files[] = ResourceStreamWrapper.inc
files[] = ResourcePublicStreamWrapper.inc
files[] = ResourcePrivateStreamWrapper.inc
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment