/** * @param string $name * @param array $values * * @return string */ public function render($name, array $values = array()) { foreach ($this->locations as $location) { $path = $location . DIRECTORY_SEPARATOR . $this->normalizeLocation($name, true) . '.tpl'; if ($this->filesystem->pathExists($path)) { return $this->renderString($this->filesystem->getFileContents($path), $values); } } }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $methodName = $data['name']; $arguments = $data['arguments']; $content = $this->getContent($resource, $methodName, $arguments); $code = $this->appendMethodToCode($this->filesystem->getFileContents($filepath), $content); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln(sprintf("<info>Method <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $methodName), 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $methodName = $data['name']; $arguments = $data['arguments']; $content = $this->getContent($resource, $methodName, $arguments); $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been created.</info>", $resource->getSrcClassname(), $methodName), 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $filepath = $resource->getSrcFilename(); if (!($content = $this->templates->render('private-constructor', array()))) { $content = $this->templates->renderString($this->getTemplate(), array()); } $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln("<info>Private constructor has been created.</info>\n", 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $filepath = $resource->getSrcFilename(); if (!($content = $this->templates->render('private-constructor', array()))) { $content = $this->templates->renderString($this->getTemplate(), array()); } $code = $this->filesystem->getFileContents($filepath); $code = $this->codeWriter->insertMethodFirstInClass($code, $content); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln("<info>Private constructor has been created.</info>\n", 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $destination = $this->getSavePath($resource); if (file_exists($destination) && !$this->io->askConfirmation(sprintf('File "%s" already exists. Overwrite?', basename($destination)), false)) { return; } $directory = dirname($destination); if (!file_exists($directory)) { $this->createDir($directory); } $code = $this->generateCodeForResource($resource, $data); $this->filesystem->putFileContents($destination, $code); $this->io->writeln($this->getPromptMessage($resource, $resource->getSrcFilename())); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = count($arguments) ? '$argument' . implode(', $argument', range(1, count($arguments))) : ''; $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $code = $this->filesystem->getFileContents($filepath); $this->filesystem->putFileContents($filepath, $this->getUpdatedCode($name, $content, $code)); $this->io->writeln(sprintf("<info>Method <value>%s::%s()</value> has been created.</info>\n", $resource->getSrcClassname(), $name), 2); }
/** * @param ResourceInterface $resource * @param array $data */ public function generate(ResourceInterface $resource, array $data) { $method = $data['method']; $expected = $data['expected']; $code = $this->filesystem->getFileContents($resource->getSrcFilename()); $values = array('%constant%' => var_export($expected, true)); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $pattern = '/' . '(function\\s+' . preg_quote($method, '/') . '\\s*\\([^\\)]*\\))\\s+{[^}]*?}/'; $replacement = '$1' . $content; $modifiedCode = preg_replace($pattern, $replacement, $code); $this->filesystem->putFileContents($resource->getSrcFilename(), $modifiedCode); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been modified.</info>", $resource->getSrcClassname(), $method), 2); }
/** * @param Resource $resource * @param array $data * * @return mixed */ public function generate(Resource $resource, array $data = array()) { $filepath = $resource->getSrcFilename(); $name = $data['name']; $arguments = $data['arguments']; $argString = $this->argumentBuilder->buildFrom($arguments); $values = array('%name%' => $name, '%arguments%' => $argString); if (!($content = $this->templates->render('method', $values))) { $content = $this->templates->renderString($this->getTemplate(), $values); } $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); $this->io->writeln(sprintf("\n<info>Method <value>%s::%s()</value> has been created.</info>", $resource->getSrcClassname(), $name), 2); }
/** * @param string $xml */ private function writeConfigFile($xml) { if (!$this->filesystem->isDirectory($this->directory)) { $this->filesystem->makeDirectory($this->directory); } $this->filesystem->putFileContents($this->getFilePath(), $xml); }
private function findSpecClassname($path) { // Find namespace and class name $namespace = ''; $content = $this->filesystem->getFileContents($path); $tokens = token_get_all($content); $count = count($tokens); for ($i = 0; $i < $count; $i++) { if ($tokens[$i][0] === T_NAMESPACE) { for ($j = $i + 1; $j < $count; $j++) { if ($tokens[$j][0] === T_STRING) { $namespace .= $tokens[$j][1] . '\\'; } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') { break; } } } if ($tokens[$i][0] === T_CLASS) { for ($j = $i + 1; $j < $count; $j++) { if ($tokens[$j] === '{') { return $namespace . $tokens[$i + 2][1]; } } } } // No class found return null; }
/** * @param $fullSpecPath * * @return array */ private function createResourcesFromSpecFiles($fullSpecPath) { $resources = array(); foreach ($this->filesystem->findPhpFilesIn($fullSpecPath) as $file) { $specFile = $file->getRealPath(); $resources[] = $this->createResourceFromSpecFile($specFile, $fullSpecPath); } return $resources; }
/** * @param string $query * * @return string */ private function getQueryPath($query) { $sepr = DIRECTORY_SEPARATOR; $replacedQuery = str_replace(array('\\', '/'), $sepr, $query); if ($this->queryContainsQualifiedClassName($query)) { $namespacedQuery = null === $this->psr4Prefix ? $replacedQuery : substr($replacedQuery, strlen($this->srcNamespace)); $path = $this->fullSpecPath . $namespacedQuery . 'Spec.php'; if ($this->filesystem->pathExists($path)) { return $path; } } return rtrim(realpath($replacedQuery), $sepr); }
/** * @param string $path * * @return PSR0Resource[] */ protected function findSpecResources($path) { if (!$this->filesystem->pathExists($path)) { return array(); } if ('.php' === substr($path, -4)) { return array($this->createResourceFromSpecFile(realpath($path))); } $resources = array(); foreach ($this->filesystem->findPhpFilesIn($path) as $file) { $resources[] = $this->createResourceFromSpecFile($file->getRealPath()); } return $resources; }
/** * @param string $filePath * @return bool */ private function fileExists($filePath) { return $this->filesystem->pathExists($filePath); }
/** * @param ResourceInterface $resource * @param string $filepath */ private function generateFileAndRenderTemplate(ResourceInterface $resource, $filepath) { $content = $this->renderTemplate($resource, $filepath); $this->filesystem->putFileContents($filepath, $content); $this->io->writeln($this->getGeneratedMessage($resource, $filepath)); }
/** * @param string $filepath * @param string $content */ private function insertMethodSignature($filepath, $content) { $code = $this->filesystem->getFileContents($filepath); $code = preg_replace('/}[ \\n]*$/', rtrim($content) . "\n}\n", trim($code)); $this->filesystem->putFileContents($filepath, $code); }
private function moduleFileExists($moduleName) { return $this->fileSystem->pathExists($this->getFilePath($moduleName)); }