You are here

class Resource in D7 Media 6

Base Resource class.

The drupal_file class represents a physical file stored in Drupal's 'File System Path'. It is important to understand that copy, move, delete, etc method affect both the the physical file the object represents and the database record for the file.

Example Factory Usage:

$factory = new Resource(); $file = $factory->load_id(87); $another = $file->load_path('images/logo.png');

$file = Resource::load_id(24);

$yet_another = $file->load('path', 'uploads/myfile.pdf');

if ($yet_another->delete()) unset($yet_another);

Hierarchy

Expanded class hierarchy of Resource

3 string references to 'Resource'
ResourceStreamCrossingTestCase::getInfo in resource/tests/ResourceStream.test
ResourceStreamPrivateTestCase::getInfo in resource/tests/ResourceStream.test
ResourceStreamTestCase::getInfo in resource/tests/ResourceStream.test

File

resource/resource.module, line 118
Resource API for Drupal, a replacement for files.

View source
class Resource {

  // @see drupal_file::load()
  protected static $files = array();
  protected static $streamWrapper;
  protected static $streamManager;
  protected $rid = 0;
  protected $uid = 1;
  protected $name = '';
  protected $url = '';
  protected $mime = 'application/octet-stream';
  protected $size = 0;
  protected $status = 0;
  protected $timestamp = 0;
  protected $htmlUrl = '';

  /**
   * drupal_file constructor.
   *
   * @param object $file (optional) stdClass object to initialize self with.
   */
  protected function __construct($resource) {
    $this->streamManager = ResourceStreamWrapperManager::singleton();
    $class = $this->streamManager
      ->class(parse_url($url, PHP_URL_SCHEME));
    $this->streamWrapper = new $class();
    $this->rid = isset($resource->rid) ? $resource->rid : 0;
    $this->url = $resource->url;
    $this->mime = $resoruce->mime;
    $this->size = $resource->size;
    $this->name = $resource->name;
    $this->htmlUrl = $this->streamWrapper
      ->htmlUrl($this->url);
  }

  /**
   * Create a new name for a resource by appending a number if a resource
   * exists at $destination.
   */
  function _destination($destination, $replace = FILE_EXISTS_RENAME) {
    if ($replace & FILE_EXISTS_REPLACE) {
      return true;
    }
    if (!file_exists($destination)) {
      return $destination;
    }
    if ($replace & FILE_EXISTS_ERROR) {
      return FALSE;
    }
    $streamdir = dirname($destination);
    $basename = basename($destination);

    // Destination file already exists, generate an alternative.
    $pos = strrpos($basename, '.');
    if ($pos !== FALSE) {
      $name = substr($basename, 0, $pos);
      $ext = substr($basename, $pos);
    }
    else {
      $name = $basename;
      $ext = '';
    }
    $counter = 0;
    do {
      $destination = $streamdir . '/' . $name . '_' . $counter++ . $ext;
    } while (file_exists($destination));
    return $destination;
  }

  /**
   * Create a copy a Resource.
   *
   * @param  string $destination (optional) @see file_copy.
   * @param int $replace (optional) @see file_destination
   * @return object|bool  drupal_file if the copy is successful, or FALSE
   */
  function copy($destination, $replace = FILE_EXISTS_RENAME) {
    $destination = $this
      ->_destination($destination, $replace);
    if (copy($this->url, $destination)) {
      $copy = clone $this;
      $copy->rid = NULL;
      $copy->url = $destination;
      if ($copy
        ->save()) {
        module_invoke_all('resource_copy', $this, $copy);
        return ResourceFactory::loadId($copy->rid);
      }
    }
    return FALSE;
  }

  /**
   * Delete a file and its database record.
   *
   * @param $force
   *   Boolean indicating that the file should be deleted even if
   *   hook_file_references() reports that the file is in use.
   * @return mixed
   *   TRUE for success, array for reference count, or FALSE in the event
   *   of an error.
   *
   * @see hook_file_references()
   */
  function delete($force = FALSE) {

    // If any module returns a value from the reference hook, the
    // file will not be deleted from Drupal, but file_delete will
    // return a populated array that tests as TRUE.
    if (!$force && ($references = module_invoke_all('resource', $this))) {
      return $references;
    }

    // Let other modules clean up on delete.
    module_invoke_all('resource_delete', $this);

    // Make sure the file is deleted before removing its row from the
    // database, so UIs can still find the file in the database.
    if (unlink($this->url)) {
      db_query('DELETE FROM {resource} WHERE rid = %d', $this->rid);

      // remove internally used static cache entries.
      $this
        ->reset_cache('rid::' . $this->rid);
      $this
        ->reset_cache('url::' . $this->url);
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Move a Resource.
   *
   * @param  string $destination (optional) @see file_copy.
   * @param int $replace (optional) @see file_destination
   * @return bool
   * @see file_copy()
   */
  function move($destination, $replace = FILE_EXISTS_RENAME) {
    $destination = $this
      ->_destination($destination, $rename);
    if (copy($this->url, $destination)) {
      unlink($this->url);
      $orig = clone $this;
      $this->url = $destination;
      if ($this
        ->save()) {
        module_invoke_all('resource_move', $this, $orig);
        return ResourceFactory::loadId($this->rid);
      }
    }
    return FALSE;
  }

  /**
   * Save the current state of a drupal_file in the database.
   * If the file->fid is empty a new database record will be added.
   *
   * @return bool TRUE if save succeeded, FALSE if save failed.
   */
  function save() {
    $this->timestamp = time();
    $manager = DrupalStreamWrapperManager::singleton();
    $class = $manager
      ->streamWrapperClass(parse_url($this->url, PHP_URL_SCHEME));
    if ($class != get_class($this
      ->streamWrapperManager())) {
      $this->streamWrapperHandler = new $class();
    }
    $this->size = $this->streamWrapperHandler
      ->size($this->url);
    $this->mime = $this->streamWrapperHandler
      ->mime($this->url);
    if (empty($this->fid)) {
      $result = drupal_write_record('resource', $this);
      module_invoke_all('resource_insert', $this);
    }
    else {
      $result = drupal_write_record('resource', $this, 'rid');
      module_invoke_all('resource_update', $this);
    }
    return $result;
  }

  /**
   * Mark a file as permanent.
   *
   * @return bool
   */
  function set_status($bitmask = NULL) {
    if (is_null($bitmask)) {
      return $file->status;
    }
    elseif (db_query('UPDATE {files} SET status=%d', $bitmask, $this->fid)) {
      $this->status = $status;
      module_invoke_all('file_set_status', $this, $status);
      return TRUE;
    }
    return FALSE;
  }
  function refresh() {
    $this->htmlUrl = $this->streamWrapper
      ->htmlUrl($this->url);
    $this->size = $this->streamWrapper
      ->size($this->url);
    $this->mime = $this->streamWrapper
      ->mime($this->url);
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Resource::$files protected static property
Resource::$htmlUrl protected property
Resource::$mime protected property
Resource::$name protected property
Resource::$rid protected property
Resource::$size protected property
Resource::$status protected property
Resource::$streamManager protected static property
Resource::$streamWrapper protected static property
Resource::$timestamp protected property
Resource::$uid protected property
Resource::$url protected property
Resource::copy function Create a copy a Resource.
Resource::delete function Delete a file and its database record.
Resource::move function Move a Resource. 1
Resource::refresh function
Resource::save function Save the current state of a drupal_file in the database. If the file->fid is empty a new database record will be added. 1
Resource::set_status function Mark a file as permanent.
Resource::_destination function Create a new name for a resource by appending a number if a resource exists at $destination.
Resource::__construct protected function drupal_file constructor.