You are here

public static function ComponentFactory::get in Module Object Oriented Programming API 6

Same name and namespace in other branches
  1. 6.2 component/moopapi.component.inc \ComponentFactory::get()
  2. 7.2 component/moopapi.component.inc \ComponentFactory::get()
  3. 7 component/moopapi.component.inc \ComponentFactory::get()

Parameters

string $app_name:

string $type:

string $id:

array $decorators:

Return value

\decorator_class

9 calls to ComponentFactory::get()
Application::getController in component/moopapi.component.inc
ApplicationAdapter::getController in component/decorator/moopapi.adapter.inc
ApplicationAdapter::getController in component/decorator/adapter/moopapi.adapter.inc
ApplicationLogger::getController in component/decorator/moopapi.logger.inc
ApplicationLogger::getController in component/decorator/logger/moopapi.logger.inc

... See full list

File

component/moopapi.component.inc, line 47

Class

ComponentFactory
An Abstract Factory for Components.

Code

public static function get($app_name, $type, $id, $decorators = array(), &$relations = array()) {

  // Template of classname for replacings.
  $class_pattern = '%app_name%id%type';
  $class_search = array(
    '%app_name',
    '%id',
    '%type',
  );

  // Switch to determine an object of which class to initialize.
  switch ($id) {
    case Component::ID_APPLICATION:
      $class_replace = array(
        $app_name,
        '',
        '',
      );
      break;
    default:
      $class_replace = array(
        $app_name,
        $id,
        $type,
      );
      break;
  }
  $class = str_replace($class_search, $class_replace, $class_pattern);
  $decorators_applied = $decorators;
  $component = new $class($decorators_applied, $relations);

  // Some preparations before building.
  $app_name_lower = strtolower($app_name);
  $type_lower = strtolower($type);
  $id_lower = strtolower($id);

  // Wrap with major version adapters.
  switch (moopapi_get_major_version()) {
    case '6':

      // Have to define explicitly because of passing by reference to array_unshift.
      if (empty($decorators)) {
        $decorators = array();
      }
      array_unshift($decorators, 'D6Adapter');

    // No break since we want to fall through.

    //break;
    case '7':
    default:
  }

  // Template of a file name with path for replacings.
  $file_pattern = '%type/%id/decorator/%decorator/%app_name.%id.%type.%decorator';
  $file_search = array(
    '%type',
    '%id',
    '%decorator',
    '%app_name',
  );
  foreach ($decorators as $did => $decorator) {
    $decorator_lower = strtolower($decorator);

    // Switch to determine where to find a necessary file.
    $file_replace = array(
      $type_lower,
      $id_lower,
      $decorator_lower,
      $app_name_lower,
    );
    $file = str_replace($file_search, $file_replace, $file_pattern);

    // Include necessary file.
    $module = $app_name_lower;
    $path = "./" . drupal_get_path('module', $module) . "/{$file}.inc";
    if (file_exists($path)) {
      module_load_include('inc', $module, $file);
    }

    // Decorate if possible.
    $decorator_class = $class . $decorator;
    if (class_exists($decorator_class)) {
      $component = new $decorator_class($decorators_applied, $relations, $component);
    }
    unset($decorators_applied[$did]);
  }
  return $component;
}