/** * Gets all the vars in this block, mapped by $VarName => $Type. * Highly opinionated. Based on the convention that all template methods * are UpperCamelCase. * @return array */ public function getVars() { $dbFieldFunctions = $this->reflector->getDBFieldFunctions(); if ($this->vars != null) { return $this->vars; } $vars = array(); $counts = array(); $booleans = $this->getPossibleBooleans(); $search = $this->getTopLevelContent(); preg_match_all("/\\\$[A-Za-z0-9._]+/", $search, $variables); if ($variables || $booleans) { foreach (reset($variables) as $m) { $label = str_replace("\$", "", $m); // If using the dot syntax, this may be a has_one, or an invocation of a DBField method. if (stristr($label, ".") !== false) { list($relation, $name) = explode('.', $label); $name = preg_replace('/\\(.*\\)/', '', $name); // The variable is a core template accessor. Move on. if (in_array(strtolower($relation), $this->reflector->getTemplateAccessors())) { continue; } // The method being called against the variable is a DBField function. // Use that information to assign a probable FieldType if (array_key_exists($name, $dbFieldFunctions)) { $class = $dbFieldFunctions[$name]; $vars[$relation] = $dbFieldFunctions[$name]; } elseif (is_subclass_of($relation, 'ViewableData')) { $vars[$relation] = $relation; } else { $vars[$relation] = "has_one"; } } else { if (!isset($counts[$label])) { $counts[$label] = 0; } $counts[$label]++; if (!in_array($label, $vars) && !$this->getChildByName($label)) { // This is a <% with %> block, and it's not using a common template accessor, // e.g. $Up, so we can make a guess about the datatype of this variable. if (!$this->isLoop() && !in_array(strtolower($label), $this->reflector->getTemplateAccessors())) { $vars[$label] = $this->reflector->inferDatatype($label); } else { if ($this->isLoop() && !in_array(strtolower($label), $this->reflector->getListFunctions())) { $vars[$label] = $this->reflector->inferDatatype($label); } } } } } } foreach ($booleans as $b) { if (isset($counts[$b])) { $counts[$b]--; if ($counts[$b] == 0) { $vars[$b] = 'Boolean'; } } } return $this->vars = $vars; }