/** * Formats PHP statement. * @return string * @internal */ public function formatPhp($statement, $args) { array_walk_recursive($args, function (&$val) { if ($val instanceof Statement) { $val = self::literal($this->formatStatement($val)); } elseif ($val === $this) { $val = self::literal('$this'); } elseif ($val instanceof ServiceDefinition) { $val = '@' . current(array_keys($this->getDefinitions(), $val, TRUE)); } if (!is_string($val)) { return; } elseif (substr($val, 0, 2) === '@@') { $val = substr($val, 1); } elseif (substr($val, 0, 1) === '@') { $pair = explode('::', $val, 2); $name = $this->getServiceName($pair[0]); if (isset($pair[1]) && preg_match('#^[A-Z][A-Z0-9_]*\\z#', $pair[1], $m)) { $val = $this->getDefinition($name)->getClass() . '::' . $pair[1]; } else { if ($name === self::THIS_CONTAINER) { $val = '$this'; } elseif ($name === $this->currentService) { $val = '$service'; } else { $val = $this->formatStatement(new Statement(['@' . self::THIS_CONTAINER, 'getService'], [$name])); } $val .= isset($pair[1]) ? PhpHelpers::formatArgs('->?', [$pair[1]]) : ''; } $val = self::literal($val); } }); return PhpHelpers::formatArgs($statement, $args); }
/** * Formats PHP statement. * @return string */ public function formatPhp($statement, $args) { $that = $this; array_walk_recursive($args, function (&$val) use($that) { if ($val instanceof Statement) { $val = ContainerBuilder::literal($that->formatStatement($val)); } elseif ($val === $that) { $val = ContainerBuilder::literal('$this'); } elseif ($val instanceof ServiceDefinition) { $val = '@' . current(array_keys($that->definitions, $val, TRUE)); } elseif (is_string($val) && preg_match('#^[\\w\\\\]*::[A-Z][A-Z0-9_]*\\z#', $val, $m)) { $val = ContainerBuilder::literal(ltrim($val, ':')); } if (is_string($val) && substr($val, 0, 1) === '@') { $pair = explode('::', $val, 2); $name = $that->getServiceName($pair[0]); if (isset($pair[1]) && preg_match('#^[A-Z][A-Z0-9_]*\\z#', $pair[1], $m)) { $val = $that->definitions[$name]->class . '::' . $pair[1]; } else { if ($name === ContainerBuilder::THIS_CONTAINER) { $val = '$this'; } elseif ($name === $that->currentService) { $val = '$service'; } else { $val = $that->formatStatement(new Statement(array('@' . ContainerBuilder::THIS_CONTAINER, 'getService'), array($name))); } $val .= isset($pair[1]) ? PhpHelpers::formatArgs('->?', array($pair[1])) : ''; } $val = ContainerBuilder::literal($val); } }); return PhpHelpers::formatArgs($statement, $args); }
/** @return Method */ public function addBody($statement, array $args = NULL) { $this->body .= (func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement) . "\n"; return $this; }
/** * Formats PHP statement. * @return string * @internal */ public function formatPhp($statement, $args) { array_walk_recursive($args, function (&$val) { if ($val instanceof Statement) { $val = new PhpLiteral($this->formatStatement($val)); } elseif (is_string($val) && substr($val, 0, 2) === '@@') { // escaped text @@ $val = substr($val, 1); } elseif (is_string($val) && substr($val, 0, 1) === '@' && strlen($val) > 1) { // service reference $name = substr($val, 1); if ($name === ContainerBuilder::THIS_CONTAINER) { $val = new PhpLiteral('$this'); } elseif ($name === $this->currentService) { $val = new PhpLiteral('$service'); } else { $val = new PhpLiteral($this->formatStatement(new Statement(['@' . ContainerBuilder::THIS_CONTAINER, 'getService'], [$name]))); } } }); return PhpHelpers::formatArgs($statement, $args); }