/** * Formats PHP statement. * @return string * @internal */ 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->getDefinitions(), $val, TRUE)); } if (!is_string($val)) { return; } elseif (substr($val, 0, 2) === '@@') { $val = substr($val, 1); } elseif (substr($val, 0, 1) === '@' && strlen($val) > 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->getDefinition($name)->getClass() . '::' . $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); }
/** * Removes ... and process constants recursively. * @return array */ public static function filterArguments(array $args) { foreach ($args as $k => $v) { if ($v === '...') { unset($args[$k]); } elseif (is_string($v) && preg_match('#^[\\w\\\\]*::[A-Z][A-Z0-9_]*\\z#', $v, $m)) { $args[$k] = ContainerBuilder::literal(ltrim($v, ':')); } elseif (is_array($v)) { $args[$k] = self::filterArguments($v); } elseif ($v instanceof Statement) { $tmp = self::filterArguments(array($v->getEntity())); $args[$k] = new Statement($tmp[0], self::filterArguments($v->arguments)); } } return $args; }