You are here

function features_export_render in Features 7

Same name and namespace in other branches
  1. 6 features.export.inc \features_export_render()
  2. 7.2 features.export.inc \features_export_render()

Render feature export into an array representing its files.

Parameters

$export: An exported feature definition.

$module_name: The name of the module to be exported.

$reset: Boolean flag for resetting the module cache. Only set to true when doing a final export for delivery.

Return value

array of info file and module file contents.

2 calls to features_export_render()
features_export_build_form_submit in ./features.admin.inc
Form submission handler for features_export_form().
_drush_features_export in ./features.drush.inc
Write a module to the site dir.
3 string references to 'features_export_render'
FeaturesCtoolsIntegrationTest::testModuleEnable in tests/features.test
Run test.
features_export_render_hooks in ./features.export.inc
Generate an array of hooks and their raw code.
features_get_normal in ./features.export.inc
Get normal objects for a given module/component pair.

File

./features.export.inc, line 231

Code

function features_export_render($export, $module_name, $reset = FALSE) {
  $code = array();

  // Generate hook code
  $component_hooks = features_export_render_hooks($export, $module_name, $reset);
  $components = features_get_components();

  // Group component code into their respective files
  foreach ($component_hooks as $component => $hooks) {
    $file = array(
      'name' => 'features',
    );
    if (isset($components[$component]['default_file'])) {
      switch ($components[$component]['default_file']) {
        case FEATURES_DEFAULTS_INCLUDED:
          $file['name'] = "features.{$component}";
          break;
        case FEATURES_DEFAULTS_CUSTOM:
          $file['name'] = $components[$component]['default_filename'];
          break;
      }
    }
    if (!isset($code[$file['name']])) {
      $code[$file['name']] = array();
    }
    foreach ($hooks as $hook_name => $hook_info) {
      $hook_code = is_array($hook_info) ? $hook_info['code'] : $hook_info;
      $hook_args = is_array($hook_info) && !empty($hook_info['args']) ? $hook_info['args'] : '';
      $hook_file = is_array($hook_info) && !empty($hook_info['file']) ? $hook_info['file'] : $file['name'];
      $code[$hook_file][$hook_name] = features_export_render_defaults($module_name, $hook_name, $hook_code, $hook_args);
    }
  }

  // Finalize strings to be written to files
  $code = array_filter($code);
  foreach ($code as $filename => $contents) {
    $code[$filename] = "<?php\n/**\n * @file\n * {$module_name}.{$filename}.inc\n */\n\n" . implode("\n\n", $contents) . "\n";
  }

  // Generate info file output
  $export = features_export_prepare($export, $module_name, $reset);
  $code['info'] = features_export_info($export);

  // Prepare the module
  // If module exists, let it be and include it in the files
  if ($existing = features_get_modules($module_name, TRUE)) {
    $code['module'] = file_get_contents($existing->filename);

    // If the current module file does not reference the features.inc include,
    // set a warning message.
    // @TODO this way of checking does not account for the possibility of inclusion instruction being commented out.
    if (isset($code['features']) && strpos($code['module'], "{$module_name}.features.inc") === FALSE) {
      features_log(t('@module does not appear to include the @include file.', array(
        '@module' => "{$module_name}.module",
        '@include' => "{$module_name}.features.inc",
      )), 'warning');
    }

    // Deprecated files. Display a message for any of these files letting the
    // user know that they may be removed.
    $deprecated = array(
      "{$module_name}.defaults",
      "{$module_name}.features.views",
      "{$module_name}.features.node",
    );
    foreach (file_scan_directory(drupal_get_path('module', $module_name), '/.*/') as $file) {
      if (in_array($file->name, $deprecated, TRUE)) {
        features_log(t('The file @filename has been deprecated and can be removed.', array(
          '@filename' => $file->filename,
        )), 'status');
      }
      elseif ($file->name === "{$module_name}.features" && empty($code['features'])) {
        $code['features'] = "<?php\n\n// This file is deprecated and can be removed.\n// Please remove include_once('{$module_name}.features.inc') in {$module_name}.module as well.\n";
      }
    }
  }
  else {
    if (!empty($code['features'])) {
      $code['module'] = "<?php\n/**\n * @file\n * Code for the {$export['name']} feature.\n */\n\ninclude_once '{$module_name}.features.inc';\n";
    }
    else {
      $code['module'] = "<?php\n/**\n * @file\n * Drupal needs this blank file.\n */\n";
    }
  }
  return $code;
}