You are here

abstract class Recipe in Realistic Dummy Content 8

Hierarchy

  • class \Drupal\realistic_dummy_content_api\recipes\Recipe

Expanded class hierarchy of Recipe

2 files declare their use of Recipe
RealisticDummyContent.php in api/src/facade/RealisticDummyContent.php
Define autoload class.
realistic_dummy_content.recipe.inc in realistic_dummy_content/recipe/realistic_dummy_content.recipe.inc
Contains a recipe for generating realistic dummy content

File

api/src/recipes/Recipe.php, line 11
Define autoload class.

Namespace

Drupal\realistic_dummy_content_api\recipes
View source
abstract class Recipe {
  static $log;
  static function Run($log) {
    self::StartTime('run');
    self::$log = $log;
    $objects = self::FindObjects();
    foreach ($objects as $object) {
      $object
        ->_Run_();
    }
    self::$log
      ->log(t('Realistic dummy content generation operation completed in @time milliseconds', array(
      '@time' => self::StopTime('run'),
    )));
  }
  static function FindObjects() {
    $objects = array();

    // We need to cycle through all active modules and look for those
    // which contain a class module_name_realistic_dummy_content_recipe
    // in the file realistic_dummy_content/recipe/module_name.recipe.inc
    $moduleHandler = \Drupal::moduleHandler();
    $modules = $moduleHandler
      ->getModuleList();
    foreach ($modules as $module) {
      $candidate = $module . '_realistic_dummy_content_recipe';
      if (module_load_include('inc', $module, 'realistic_dummy_content/recipe/' . $module . '.recipe') && class_exists($candidate)) {
        $objects[] = new $candidate();
      }
    }
    return $objects;
  }

  /**
   * @param $more
   *   Can contain:
   *     kill => TRUE|FALSE
   */
  static function GetGenerator($type, $bundle, $count, $more) {
    if (in_array($type, array(
      'user',
      'node',
    ))) {
      if (module_exists('devel_generate')) {
        return new DevelGenerateGenerator($type, $bundle, $count, $more);
      }
      else {
        self::$log
          ->Error(t('Please enable devel\'s devel_generate module to generate users or nodes.'));
      }
    }
    else {
      self::$log
        ->Error(t('Entity types other than user and node are not supported for realistic dummy content recipe.'));
    }
  }
  function NewEntities($type, $bundle, $count, $more = array()) {
    self::StartTime(array(
      $type,
      $bundle,
      $count,
    ));
    if ($generator = self::GetGenerator($type, $bundle, $count, $more)) {
      $generator
        ->Generate();
    }
    else {
      self::$log
        ->Error(t('Could not find a generator for @type @bundle.', array(
        '@type' => $type,
        '@bundle' => $bundle,
      )));
    }
    $time = self::StopTime(array(
      $type,
      $bundle,
      $count,
    ));
    self::$log
      ->log(t('@type @bundle: @n created in @time milliseconds', array(
      '@type' => $type,
      '@bundle' => $bundle,
      '@n' => $count,
      '@time' => $time,
    )));
  }
  static function StartTime($id) {
    \Drupal\Component\Utility\Timer::start(serialize($id));
  }
  static function StopTime($id) {
    $timer = \Drupal\Component\Utility\Timer::stop(serialize($id));
    return $timer['time'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Recipe::$log static property
Recipe::FindObjects static function
Recipe::GetGenerator static function
Recipe::NewEntities function
Recipe::Run static function
Recipe::StartTime static function
Recipe::StopTime static function