public function build() { if (!$this->start) { die("DojoExecutedFunction->build() used before setting a start position"); } if ($this->end) { return $this->end; } $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1]); $line = $lines[$this->start[0]]; $this->start = array($this->start[0], strpos($line, 'function')); $this->end = parent::build(); // Basically, the end array here will hold the position of the final } in the function declaration. $lines = Text::chop($this->package->getCode(), $this->end[0], $this->end[1], false, false, true); $closed = false; foreach ($lines as $line_number => $line) { $offset = 0; if ($line_number == $this->end[0]) { $offset = $this->end[1]; } if (preg_match('%\\S%', $line, $match, PREG_OFFSET_CAPTURE, $offset)) { if (!$closed) { if ($match[0][0] != ')') { return false; } else { $closed = true; $offset = $match[0][1] + 1; } } } if (preg_match('%\\S%', $line, $match, PREG_OFFSET_CAPTURE, $offset)) { if ($closed) { if ($match[0][0] != '(') { return false; } else { $parameters = new DojoParameters($this->package, $line_number, $match[0][1]); $end = $parameters->build(); break; } } } } return $end; }
public function removeCodeFrom($lines) { $keys = array_keys($lines); $first = array_shift($keys); $last = array_pop($keys); for ($i = $first; $i <= $last; $i++) { $line = $lines[$i]; if (preg_match('%function\\s*\\([^)]*\\)\\s*{%', $line, $match, PREG_OFFSET_CAPTURE)) { $declaration = new DojoFunctionDeclare($this, $i, $match[0][1]); list($i, ) = $declaration->build(); $lines = $declaration->removeCodeFrom($lines); } elseif (preg_match('%^.*(with|switch)\\s*\\([^(]*\\)\\s*{%', $line, $match)) { $with_lines = Text::chop($lines, $i, strlen($match[0]) - 1, null, null, true); list($end_line, $end_pos) = Text::findTermination($with_lines, '}', '{}()[]'); for ($j = $i; $j <= $end_line; $j++) { $line = $lines[$j]; if ($j == $i) { $lines[$j] = Text::blankOutAt($line, strlen($match[0]) - 1); } elseif ($j == $end_line) { $lines[$j] = Text::blankOutAt($line, 0, $end_pos); } else { $lines[$j] = Text::blankOut($line, $line); } } } } return $lines; }
public function getInstanceFunctions($function_name) { $functions = array(); $this->build(); $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true); foreach ($lines as $line_number => $line) { if (preg_match('%\\bthis\\.([a-zA-Z0-9._$]+)\\s*=\\s*function\\b%', $line, $match, PREG_OFFSET_CAPTURE)) { $function = new DojoFunctionDeclare($this->package, $line_number, $match[0][1]); $function->setFunctionName($function_name); $end = $function->build(); $functions[] = $function; } } return $functions; }
public function build() { if (!$this->start) { die("DojoExecutedFunction->build() used before setting a start position"); } if ($this->end) { return $this->end; } $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1]); $line = $lines[$this->start[0]]; $this->start = array($this->start[0], strpos($line, 'function')); $this->end = parent::build(); // Basically, the end array here will hold the position of the final } in the function declaration. $parameters = $this->getParameters(); if (count($parameters) && ($pos = strpos($lines[$this->end[0]], '(')) !== false) { $arguments = new DojoParameters($this->package); $arguments->start = array($this->end[0], $pos); $arguments->build(); $arguments = $arguments->getParameters(); foreach ($parameters as $pos => $parameter) { if ($arguments[$pos]) { $argument = $arguments[$pos]; if ($argument->isA(DojoVariable) && $parameter->isA(DojoVariable)) { if (preg_match('%(^|\\|\\|)([a-zA-Z0-9_.$]+)(\\|\\||$)%', $argument->getVariable(), $match)) { $this->body->addResolvedParameter($parameter->getVariable(), $match[2]); } } } } } $lines = Text::chop($this->package->getCode(), $this->end[0], $this->end[1], false, false, true); $closed = false; foreach ($lines as $line_number => $line) { $offset = 0; if ($line_number == $this->end[0]) { $offset = $this->end[1]; } if (preg_match('%\\S%', $line, $match, PREG_OFFSET_CAPTURE, $offset)) { if (!$closed) { if ($match[0][0] != ')') { return false; } else { $closed = true; $offset = $match[0][1] + 1; } } } if (preg_match('%\\S%', $line, $match, PREG_OFFSET_CAPTURE, $offset)) { if ($closed) { if ($match[0][0] != '(') { return false; } else { $parameters = new DojoParameters($this->package, $line_number, $match[0][1]); $end = $parameters->build(); break; } } } } return $end; }