You are here

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

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

Parameters

string $app_name:

string $type:

string $id:

array $decorators:

Return value

\decorator_class

6 calls to ComponentFactory::get()
Application::getController in component/moopapi.component.inc
ApplicationAdapter::getController in component/decorator/adapter/moopapi.adapter.inc
ApplicationLogger::getController in component/decorator/logger/moopapi.logger.inc
Controller::getModel in component/moopapi.component.inc
ControllerLogger::getModel 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':
      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;

    // Apply decorator only if it is explicitly defined.
    $path = "./" . drupal_get_path('module', $module) . "/{$file}.inc";
    if (file_exists($path)) {
      module_load_include('inc', $module, $file);
    }

    // If there is no such file, we assume that it was included somewhere else.
    $decorator_class = $class . $decorator;
    $component = new $decorator_class($decorators_applied, $relations, $component);
    unset($decorators_applied[$did]);
  }
  return $component;
}