You are here

class ClientsHandlerEntityController in Web Service Clients 7.3

A controller for entities that function as handlers.

Not specific to Clients module -- could be abstracted.

The main job of this controller class is to implement a factory-by-row pattern, that is, where the class of each object returned by a load is determined by the value of a database column, in this case, its type.

Hierarchy

Expanded class hierarchy of ClientsHandlerEntityController

1 string reference to 'ClientsHandlerEntityController'
clients_entity_info in ./clients.module
Implements hook_entity_info().

File

includes/clients.controller.inc, line 18
Provides a controller building upon the Entity exportable controller but providing features for handler objects.

View source
class ClientsHandlerEntityController extends EntityAPIControllerExportable {

  /**
   * Overridden to not use 'entity class': our class is variable.
   *
   * @return The results in a Traversable object.
   */
  public function query($ids, $conditions, $revision_id = FALSE) {

    // Build the query.
    $query = $this
      ->buildQuery($ids, $conditions, $revision_id);
    $result = $query
      ->execute();
    return $result;
  }

  // buildQuery() takes care of loading by name as well as id.

  /**
   * Overridden to implement factory-by-row pattern.
   *
   * @see DrupalDefaultEntityController#load($ids, $conditions)
   */
  public function load($ids = array(), $conditions = array()) {

    // Disable the static cache until we are finished loading entities. This is
    // because the parent load() will want to cache the entities it loads as
    // plain entities rather than handler entities; after that's been done, we
    // can't write out handler entities to cache again without unsetting them
    // from the cache or emptying it, both of which are inefficient.
    // (We can't use $cache here, as that would disable loading from cache too.)
    $this->cache_set = FALSE;

    // Call the parent to load the entities.
    // This gets us StdClass objects if the entities were not previously cached.
    $entities = parent::load($ids, $conditions);

    // Copy the loaded entity object to the right class for its type.
    $return = array();
    foreach ($entities as $name => $plain_entity) {

      // Get the class to use.
      $class = $this
        ->getClass($plain_entity);
      if ($plain_entity instanceof $class) {

        // If the object is already of the correct class, then it was loaded
        // from the internal controller cache, and we're done.
        $return[$name] = $plain_entity;
      }
      else {

        // Otherwise, we need to remake the object to be of the handler class.
        // The connection's __construct() takes over from here.
        $entity = new $class((array) $plain_entity, $this->entityType);
        $return[$name] = $entity;
      }
    }

    // Turn static caching back on.
    $this->cache_set = TRUE;
    $this
      ->cacheSet($return);
    return $return;
  }

  /**
   * Overridden.
   * @see DrupalDefaultEntityController::cacheSet()
   */
  protected function cacheSet($entities) {

    // Only set the static cache if our own flag is set.
    // @see load().
    if ($this->cache_set) {
      parent::cacheSet($entities);
    }
  }

  /**
   * Implements EntityAPIControllerInterface.
   *
   * Overridden to implement factory-by-row pattern.
   */
  public function create(array $values = array()) {

    // Add is_new property if it is not set.
    $values += array(
      'is_new' => TRUE,
    );

    // Get the class to use.
    $class = $this
      ->getClass($values);
    return new $class($values, $this->entityType);
  }

  /**
   * Helper to get the class to create for an entity.
   *
   * Uses data in $entity_info['factory'].
   *
   * @param $entity_data
   *  Either an array or an object of entity data.
   *
   * @return
   *  The name of a class.
   */
  function getClass($entity_data) {
    if (is_object($entity_data)) {
      $type = $entity_data->type;
    }
    else {
      $type = $entity_data['type'];
    }
    $prefix = $this->entityInfo['factory']['class prefix'];
    $connection_type = clients_get_connection_types($type);
    if (empty($connection_type['class'])) {
      $class = $prefix . $type;
    }
    else {
      $class = $connection_type['class'];
    }
    if (class_exists($class)) {
      return $class;
    }
    else {
      return $this->entityInfo['factory']['broken class'];
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClientsHandlerEntityController::cacheSet protected function Overridden. Overrides EntityAPIControllerExportable::cacheSet
ClientsHandlerEntityController::create public function Implements EntityAPIControllerInterface. Overrides EntityAPIController::create
ClientsHandlerEntityController::getClass function Helper to get the class to create for an entity.
ClientsHandlerEntityController::load public function Overridden to implement factory-by-row pattern. Overrides EntityAPIControllerExportable::load
ClientsHandlerEntityController::query public function Overridden to not use 'entity class': our class is variable. Overrides EntityAPIController::query
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildContent public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::buildContent
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIControllerExportable::$entityCacheByName protected property
EntityAPIControllerExportable::$nameKey protected property
EntityAPIControllerExportable::applyConditions protected function
EntityAPIControllerExportable::attachLoad protected function Overridden. Overrides DrupalDefaultEntityController::attachLoad
EntityAPIControllerExportable::buildQuery protected function Support loading by name key. Overrides EntityAPIController::buildQuery
EntityAPIControllerExportable::cacheGet protected function Overridden. Overrides DrupalDefaultEntityController::cacheGet
EntityAPIControllerExportable::cacheGetByName protected function Like cacheGet() but keyed by name.
EntityAPIControllerExportable::delete public function Overridden to care about reverted entities. Overrides EntityAPIController::delete
EntityAPIControllerExportable::export public function Overridden. Overrides EntityAPIController::export
EntityAPIControllerExportable::invoke public function Overridden to care about reverted bundle entities and to skip Rules. Overrides EntityAPIController::invoke
EntityAPIControllerExportable::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides EntityAPIController::resetCache
EntityAPIControllerExportable::save public function Overridden to care exportables that are overridden. Overrides EntityAPIController::save
EntityAPIControllerExportable::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIController::view
EntityAPIControllerExportable::__construct public function Overridden. Overrides EntityAPIController::__construct