You are here

abstract class ComponentFactory in Module Object Oriented Programming API 7.2

Same name and namespace in other branches
  1. 6.2 component/moopapi.component.inc \ComponentFactory
  2. 6 component/moopapi.component.inc \ComponentFactory
  3. 7 component/moopapi.component.inc \ComponentFactory

An Abstract Factory for Components.

Hierarchy

Expanded class hierarchy of ComponentFactory

File

component/moopapi.component.inc, line 38

View source
abstract class ComponentFactory {

  /**
   *
   * @param string $app_name
   * @param string $type
   * @param string $id
   * @param array $decorators
   * @return \decorator_class
   */
  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;
  }

}

Members