From 5ee3384d629487ed0754cd01601938e387ac9b5d Thu, 3 Nov 2011 11:54:44 +0100
From: Bram Goffings <bramgoffings@gmail.com>
Date: Thu, 3 Nov 2011 11:51:53 +0100
Subject: [PATCH] Hacky arg handler

diff --git a/includes/Drupal/Context/Handler/HandlerArg.php b/includes/Drupal/Context/Handler/HandlerArg.php
new file mode 100644
index 0000000..85adbd6
--- /dev/null
+++ b/includes/Drupal/Context/Handler/HandlerArg.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace Drupal\Context\Handler;
+
+use \Drupal\Context\ContextInterface;
+
+/**
+ * Arg Context Handler implementation.
+ */
+class HandlerArg extends HandlerAbstract {
+
+  public function getValue(array $args = array(), ContextInterface $context = null) {
+    // $args[0] = $index    
+    // $args[1] = $path
+
+    if (isset($args[1])) {
+      $path = $args[1];
+    }
+    else {
+      $path = drupal_get_context()->getValue('path:system');
+    }
+
+    $arguments = explode('/', $path);
+
+    if (!isset($args[0]) || $args[0] < 0) {
+      return $arguments;
+    }
+    $index = $args[0];
+    if (isset($arguments[$index])) {
+      return $arguments[$index];
+    }
+  }
+}
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 8a29f3c..5c55a01 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -2756,28 +2756,22 @@
  *   the components of the current path.
  */
 function arg($index = NULL, $path = NULL) {
-  // Even though $arguments doesn't need to be resettable for any functional
-  // reasons (the result of explode() does not depend on any run-time
-  // information), it should be resettable anyway in case a module needs to
-  // free up the memory used by it.
-  // Use the advanced drupal_static() pattern, since this is called very often.
-  static $drupal_static_fast;
-  if (!isset($drupal_static_fast)) {
-    $drupal_static_fast['arguments'] = &drupal_static(__FUNCTION__);
+  $context = drupal_get_context();
+  if (isset($index)) {
+    if (!isset($path)) {
+      return $context->getValue('arg:'.$index);
+    }
+    else {
+      return $context->getValue('arg:'.$index.':'.$path);
+    }
   }
-  $arguments = &$drupal_static_fast['arguments'];
-
-  if (!isset($path)) {
-    $path = drupal_get_context()->getValue('path:system');
-  }
-  if (!isset($arguments[$path])) {
-    $arguments[$path] = explode('/', $path);
-  }
-  if (!isset($index)) {
-    return $arguments[$path];
-  }
-  if (isset($arguments[$path][$index])) {
-    return $arguments[$path][$index];
+  else {
+    if (!isset($path)) {
+      return $context->getValue('arg');
+    }
+    else {
+      return $context->getValue('arg:-1:'.$path);
+    }
   }
 }
 
diff --git a/modules/system/system.module b/modules/system/system.module
index d565958..f35ac21 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -1917,11 +1917,10 @@
  * This won't stay as the implementation, but it works for the time being.
  */
 function system_context_init(\Drupal\Context\ContextInterface $context) {
-
   $context->setHandler('http', '\Drupal\Context\Handler\HandlerHttp');
   $context->setHandler('path:raw', '\Drupal\Context\Handler\HandlerPathRaw');
   $context->setHandler('path:system', '\Drupal\Context\Handler\HandlerPathSystem');
-
+  $context->setHandler('arg', '\Drupal\Context\Handler\HandlerArg');
 }
 
 
