public function execute() { $ns = $this->extractNS($this->configuration['class']); $class = $this->extractLocal($this->configuration['class']); $doc = RootNode::create($ns); $ns = $doc->getNamespace($ns); Token::newline()->insertBefore($ns); Token::newline()->appendTo($ns); $class = ClassNode::create($class); if ($parent = $this->configuration['parent']) { Parser::parseSnippet('use ' . ltrim($parent, '\\') . ';')->appendTo($ns)->after(Token::newline()); $class->setExtends($this->extractLocal($parent)); } $interfaces = (array) $this->configuration['interfaces']; foreach ($interfaces as $interface) { Parser::parseSnippet('use ' . ltrim($interface, '\\') . ';')->appendTo($ns)->after(Token::newline()); } $class->setImplements(array_map([$this, 'extractLocal'], $interfaces)); if (isset($this->configuration['doc'])) { $class->setDocComment(DocCommentNode::create($this->configuration['doc'])); } $class->appendTo($ns)->before(Token::newline()); $destination = $this->getUnaliasedPath($this->configuration['destination']); $dir = subStr($destination, 0, strrPos($destination, '/')); $this->fs->mkdir($dir); file_put_contents($destination, $doc->getText()); // Need to store the class' local name as its index identifier because // \Pharborist\Filter::isClass() doesn't support lookup by qualified path. $this->target->getIndexer('class')->addFile($destination); }
/** * Creates a new, blank PHP source file. * * @param string|NULL $ns * If provided, the new document will have this namespace added to it. * * @return static */ public static function create($ns = NULL) { $node = new RootNode(); $node->addChild(Token::openTag()); if (is_string($ns) && $ns) { NamespaceNode::create($ns)->appendTo($node)->after(Token::newline()); } return $node; }
private function updateArray(ArrayNode $node, $data) { $i = 0; foreach ($node->getElements() as $el) { if ($el instanceof ArrayPairNode) { $k = (string) $el->getKey(); $k = trim($k, '"\''); $v = $el->getValue(); if (!isset($data[$k])) { $this->cleanAround($el); } else { if ($v instanceof ArrayNode && is_array($data[$k])) { $v = $this->updateArray($v, $data[$k]); unset($data[$k]); } else { $v->replaceWith(Parser::parseExpression($data[$k])); unset($data[$k]); } } } elseif ($el instanceof ArrayNode && (!isset($data[$i]) || is_array($data[$i]))) { if (!isset($data[$i])) { $this->cleanAround($el); } else { $this->updateArray($el, $data[$i]); unset($data[$i]); $i++; } } else { if (!isset($data[$i])) { $this->cleanAround($el); } else { $el->replaceWith(Parser::parseExpression($data[$i])); unset($data[$i]); $i++; } } } foreach ($data as $key => $val) { $v = Parser::parseExpression(self::var_codify($val)); if (!is_integer($key)) { $v = ArrayPairNode::create(Node::fromValue($key), $v); } $comma = false; $list = $node->getElementList(); $children = []; foreach ($list->children() as $child) { $children[] = $child; } $prev = end($children); if ($prev) { do { if ((string) $prev === ',') { $comma = true; break; } } while (is_object($prev) && ($prev = $prev->previous()) instanceof WhitespaceNode); } else { $comma = true; } $indent = 0; $prev = end($children); while ($prev && strpos($prev, "\n") === false) { $prev = $prev->previous(); } $indent = ''; if ($prev) { $prev = explode("\n", (string) $prev); $prev = array_pop($prev); for ($i = 0; $i < strlen($prev); $i++) { if (in_array($prev[$i], ["\t", ' '])) { $indent .= $prev[$i]; } else { break; } } } if (!$comma) { $list->append(Token::comma()); } $list->append(Token::newline()); if ($indent) { $list->append(WhitespaceNode::create($indent)); } $list->append($v); } }