/** * Initializes a package template in the provided directory. * * @throws InvalidPathException * * @param $vendor * @param $package * @param $directory */ public function initialize($vendor, $package, $directory) { if (!$this->files->exists($directory) || !$this->files->isDirectory($directory)) { throw new InvalidPathException("{$directory} does not exist or is not a valid directory."); } $packageComposer = new Package(); $packageComposer->setVendor($vendor); $packageComposer->setPackage($package); $packageComposer->setDescription('Give your package template a good description'); $packageComposer->setLicense(user_config('configuration.license', '')); $packageComposer->setAuthors(user_config('configuration.authors', [])); $writer = new ConfigurationWriter($packageComposer->toArray()); $writer['config'] = (object) ['vendor-dir' => '_newup_vendor']; $writer->save($directory . '/composer.json'); $this->renderer->setData('package', $package); $this->renderer->setData('vendor', $vendor); $packageClass = $this->renderer->render('template'); if (!$this->files->exists($directory . '/_newup/')) { $this->files->makeDirectory($directory . '/_newup/'); } if ($this->shouldCreateTemplateDirectory && $this->files->exists($directory . '/_template') == false) { $this->files->makeDirectory($directory . '/_template'); } $this->files->put($directory . '/_newup/Package.php', $packageClass); }
/** * Processes the provided path. * * @param $path * @return mixed */ public function processPath($path) { $path = $this->replaceDoubleOpeningBrackets($path); $path = $this->convertSingleOpeningSquareBracketsToPipes($path); $path = $this->replaceBracketEscapeSequenceWithSingleOpeningBrackets($path); $this->constructPathNameLexer(); $path = $this->templateRenderer->renderString($path); $this->restoreOriginalLexer(); $path = $this->removeUnwantedCharactersFromString($path); return $path; }
/** * Shares data with package template files. * * @param $key The name of the variable to share. * @param null $value The value of the variable to share. * @return $this */ public function shareData($key, $value = null) { if (is_array($key)) { foreach ($key as $variableName => $variableValue) { $this->templateRenderer->setData($variableName, $variableValue); } return $this; } $this->templateRenderer->setData($key, $value); return $this; }
/** * Adds a single path to the underlying collectors and tree generators. * * This method will add all processed paths to the internal collection. * Additionally, it will return the array of paths that were processed * during the current invocation. * * @param $path * @throws \NewUp\Exceptions\InvalidPathException * @return array */ private function addPath($path) { $paths = $this->analyzer->analyze($path); $newPaths = []; foreach ($paths as $filePath) { $newPathInformation = $filePath; $newPathInformation['original'] = $filePath['path']; if ($this->getCollectorValue($filePath['path']) !== null) { // If the value from the collector is not null, we need to process the path value // using the template renderer. $newPathInformation['path'] = $this->templateRenderer->renderString($this->getCollectorValue($filePath['path'])); } else { // If the value IS null, we need to process the path value using the path parser. $newPathInformation['path'] = $this->parser->processPath($filePath['path']); } $newPaths[] = $newPathInformation; $this->paths[] = $newPathInformation; // Add the new file path association to the collector. $this->addRawToCollector($filePath['path'], $newPathInformation['path']); } $this->templateRenderer->addPath($path); return $newPaths; }