From 192019470a0ea63340b31a6670ff4e8d1cc9c00d Mon Sep 17 00:00:00 2001
From: sumsi <sumsi@761344.no-reply.drupal.org>
Date: Thu, 3 May 2012 14:21:59 -0300
Subject: [PATCH] Issue #1348820 by fubhy, danillonunes: Reduce alpha_invoke performance impact

---
 alpha/includes/alpha.inc                      |    8 ------
 alpha/template.php                            |   33 +++++++++++++++++++++++++
 starterkits/alpha-xhtml/preprocess/README.txt |    4 +--
 starterkits/alpha-xhtml/process/README.txt    |    4 +--
 starterkits/omega-html5/preprocess/README.txt |    4 +--
 starterkits/omega-html5/process/README.txt    |    4 +--
 starterkits/omega-xhtml/preprocess/README.txt |    4 +--
 starterkits/omega-xhtml/process/README.txt    |    4 +--
 8 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/alpha/includes/alpha.inc b/alpha/includes/alpha.inc
index b4be3e2..5384297 100644
--- a/alpha/includes/alpha.inc
+++ b/alpha/includes/alpha.inc
@@ -94,14 +94,6 @@ function alpha_invoke($type, $hook, &$vars) {
   foreach (alpha_theme_trail($theme->theme) as $key => $name) {
     $function = $key . '_alpha_' . $type . '_' . $hook;
 
-    if (!function_exists($function)) {
-      $file = drupal_get_path('theme', $key) . '/' . $type . '/' . $type . '-' . str_replace('_', '-', $hook) . '.inc';
-
-      if (is_file($file)) {
-        include $file;
-      }
-    }
-
     if (function_exists($function)) {
       $function($vars);
     }
diff --git a/alpha/template.php b/alpha/template.php
index 6dda153..d40ae85 100644
--- a/alpha/template.php
+++ b/alpha/template.php
@@ -53,6 +53,39 @@ function alpha_theme($existing, $type, $theme, $path) {
 }
 
 /**
+ * Implements hook_theme_registry_alter().
+ *
+ * Allows subthemes to split preprocess / process / theme code across separate
+ * files to keep the main template.php file clean. This is really fast because
+ * it uses the theme registry to cache the pathes to the files that it finds.
+ */
+function alpha_theme_registry_alter(&$registry) {
+  $active_theme = alpha_get_theme();
+  foreach (alpha_theme_trail($active_theme->theme) as $theme => $theme_data) {
+    $path = drupal_get_path('theme', $theme);
+    foreach (array('preprocess', 'process') as $type) {
+      // Only look for files that match the '(pre)process-something.inc' pattern.
+      $mask = '/^' . $type . '-.+\.inc$/';
+      // This is the length of the prefix (e.g. 'preprocess-') of the basename
+      // of a file.
+      $type_length = strlen($type) + 1;
+
+      // Recursively scan the folder for the current step for (pre)process
+      // files and write them to the registry.
+      foreach (file_scan_directory($path . '/' . $type, $mask) as $item) {
+        $hook = substr($item->name, $type_length);
+
+        if (array_key_exists($hook, $registry)) {
+          // By adding this file to the 'includes' array we make sure that it is
+          // loaded before it is being executed.
+          $registry[$hook]['includes'][] = $path . '/' . $type . '/' . $item->filename;
+        }
+      }
+    }
+  }
+}
+
+/**
  * Implements hook_preprocess().
  */
 function alpha_preprocess(&$vars, $hook) {
diff --git a/starterkits/alpha-xhtml/preprocess/README.txt b/starterkits/alpha-xhtml/preprocess/README.txt
index ee2fcfa..6ee4c64 100644
--- a/starterkits/alpha-xhtml/preprocess/README.txt
+++ b/starterkits/alpha-xhtml/preprocess/README.txt
@@ -30,9 +30,7 @@ TEMPLATE_preprocess_comment() = preprocess-comment.inc
 TEMPLATE_preprocess_region() = preprocess-region.inc
 etc.
 
-Inside of your preprocess-HOOK.inc files, you can either directly dump the PHP code as it
-would normally appear INSIDE of a preprocess function, or you can optionally (recommended)
-wrap the code in a custom hook for Alpha/Omega as such:
+Inside of your preprocess-HOOK.inc files you must wrap the code in a custom hook for Alpha/Omega as such:
 
 function THEMENAME_alpha_preprocess_HOOK(&$vars) {
   // custom functionality here
diff --git a/starterkits/alpha-xhtml/process/README.txt b/starterkits/alpha-xhtml/process/README.txt
index 983da3e..02aca50 100644
--- a/starterkits/alpha-xhtml/process/README.txt
+++ b/starterkits/alpha-xhtml/process/README.txt
@@ -30,9 +30,7 @@ TEMPLATE_process_comment() = process-comment.inc
 TEMPLATE_process_region() = process-region.inc
 etc.
 
-Inside of your process-HOOK.inc files, you can either directly dump the PHP code as it
-would normally appear INSIDE of a process function, or you can optionally (recommended)
-wrap the code in a custom hook for Alpha/Omega as such:
+Inside of your process-HOOK.inc files you must wrap the code in a custom hook for Alpha/Omega as such:
 
 function THEMENAME_alpha_process_HOOK(&$vars) {
   // custom functionality here
diff --git a/starterkits/omega-html5/preprocess/README.txt b/starterkits/omega-html5/preprocess/README.txt
index ee2fcfa..6ee4c64 100644
--- a/starterkits/omega-html5/preprocess/README.txt
+++ b/starterkits/omega-html5/preprocess/README.txt
@@ -30,9 +30,7 @@ TEMPLATE_preprocess_comment() = preprocess-comment.inc
 TEMPLATE_preprocess_region() = preprocess-region.inc
 etc.
 
-Inside of your preprocess-HOOK.inc files, you can either directly dump the PHP code as it
-would normally appear INSIDE of a preprocess function, or you can optionally (recommended)
-wrap the code in a custom hook for Alpha/Omega as such:
+Inside of your preprocess-HOOK.inc files you must wrap the code in a custom hook for Alpha/Omega as such:
 
 function THEMENAME_alpha_preprocess_HOOK(&$vars) {
   // custom functionality here
diff --git a/starterkits/omega-html5/process/README.txt b/starterkits/omega-html5/process/README.txt
index 983da3e..02aca50 100644
--- a/starterkits/omega-html5/process/README.txt
+++ b/starterkits/omega-html5/process/README.txt
@@ -30,9 +30,7 @@ TEMPLATE_process_comment() = process-comment.inc
 TEMPLATE_process_region() = process-region.inc
 etc.
 
-Inside of your process-HOOK.inc files, you can either directly dump the PHP code as it
-would normally appear INSIDE of a process function, or you can optionally (recommended)
-wrap the code in a custom hook for Alpha/Omega as such:
+Inside of your process-HOOK.inc files you must wrap the code in a custom hook for Alpha/Omega as such:
 
 function THEMENAME_alpha_process_HOOK(&$vars) {
   // custom functionality here
diff --git a/starterkits/omega-xhtml/preprocess/README.txt b/starterkits/omega-xhtml/preprocess/README.txt
index ee2fcfa..6ee4c64 100644
--- a/starterkits/omega-xhtml/preprocess/README.txt
+++ b/starterkits/omega-xhtml/preprocess/README.txt
@@ -30,9 +30,7 @@ TEMPLATE_preprocess_comment() = preprocess-comment.inc
 TEMPLATE_preprocess_region() = preprocess-region.inc
 etc.
 
-Inside of your preprocess-HOOK.inc files, you can either directly dump the PHP code as it
-would normally appear INSIDE of a preprocess function, or you can optionally (recommended)
-wrap the code in a custom hook for Alpha/Omega as such:
+Inside of your preprocess-HOOK.inc files you must wrap the code in a custom hook for Alpha/Omega as such:
 
 function THEMENAME_alpha_preprocess_HOOK(&$vars) {
   // custom functionality here
diff --git a/starterkits/omega-xhtml/process/README.txt b/starterkits/omega-xhtml/process/README.txt
index 983da3e..02aca50 100644
--- a/starterkits/omega-xhtml/process/README.txt
+++ b/starterkits/omega-xhtml/process/README.txt
@@ -30,9 +30,7 @@ TEMPLATE_process_comment() = process-comment.inc
 TEMPLATE_process_region() = process-region.inc
 etc.
 
-Inside of your process-HOOK.inc files, you can either directly dump the PHP code as it
-would normally appear INSIDE of a process function, or you can optionally (recommended)
-wrap the code in a custom hook for Alpha/Omega as such:
+Inside of your process-HOOK.inc files you must wrap the code in a custom hook for Alpha/Omega as such:
 
 function THEMENAME_alpha_process_HOOK(&$vars) {
   // custom functionality here
-- 
1.7.3.2

