public static function run(Route $route, AdapterInterface $console, $container) { $error = 0; $oldPath = $route->getMatchedParam("old", "/tmp/bench/current.json"); $newPath = $route->getMatchedParam("new", "./athletic_output.json"); $old = json_decode(file_get_contents($oldPath), true); $new = json_decode(file_get_contents($newPath), true); $comparator = $container->get(ComparatorService::class); foreach ($new as $group => $values) { if (!array_key_exists($group, $old)) { continue; } $newAvg = (double) $values['trigger']['avg']; $oldAvg = (double) $old[$group]['trigger']['avg']; $perct = $comparator->perctDecrease($newAvg, $oldAvg); if ($perct >= 5) { $error = 1; $console->writeLine("{$group} +{$perct}%", ColorInterface::RED); } elseif ($perct < -10) { $console->writeLine("{$group} {$perct}%", ColorInterface::GREEN); } else { $console->writeLine("{$group} {$perct}%", ColorInterface::NORMAL); } } return $error; }
public function __invoke(Route $route, Console $console) : int { $basePath = $route->getMatchedParam('path'); $postsPath = $route->getMatchedParam('postsPath'); $authorsPath = $route->getMatchedParam('authorsPath'); $dbPath = $route->getMatchedParam('dbPath'); $message = 'Generating blog post database'; $length = strlen($message); $width = $console->getWidth(); $console->write($message, Color::BLUE); $pdo = $this->createDatabase($dbPath, $console); $path = sprintf('%s/%s', realpath($basePath), ltrim($postsPath)); $trim = strlen(realpath($basePath)) + 1; $parser = new Parser(null, new CommonMarkParser()); $statements = []; foreach (new MarkdownFileFilter($path) as $fileInfo) { $path = $fileInfo->getPathname(); $document = $parser->parse(file_get_contents($path)); $metadata = $document->getYAML(); $html = $document->getContent(); $parts = explode($this->postDelimiter, $html, 2); $body = $parts[0]; $extended = isset($parts[1]) ? $parts[1] : ''; $author = $this->getAuthor($metadata['author'], $authorsPath); $template = empty($statements) ? $this->initial : $this->item; $statements[] = sprintf($template, $pdo->quote($metadata['id']), $pdo->quote(substr($path, $trim)), (new DateTime($metadata['created']))->getTimestamp(), (new DateTime($metadata['updated']))->getTimestamp(), $pdo->quote($metadata['title']), $pdo->quote($author['id']), $metadata['draft'] ? 1 : 0, $metadata['public'] ? 1 : 0, $pdo->quote($body), $pdo->quote(sprintf('|%s|', implode('|', $metadata['tags'])))); } $pdo->exec(implode("\n", $statements)); return $this->reportSuccess($console, $width, $length); }
/** * Release components. * * Uses the version, exclude, basePath, and verbose (or "v") flags provided * with the route to tag the next maintenance release of all components * (with the exception of those in the exclude list). * * Changes directory to the basePath prior to tagging each component. * * @param Route $route * @param Console $console * @return int */ public function __invoke(Route $route, Console $console) { $minor = $route->getMatchedParam('version'); $version = $minor; $exclude = $route->getMatchedParam('exclude'); $path = $route->getMatchedParam('basePath'); $opts = $route->getMatches(); $this->verbose = $opts['verbose'] || $opts['v']; $this->emit(sprintf("Using git: %s\n", $this->git), $console, Color::BLUE); chdir($path); foreach ($this->components as $component) { if (in_array($component, $exclude, true)) { $this->emit(sprintf("[SKIP] %s\n", $component), $console, Color::GREEN); continue; } $this->emit(sprintf("[START] %s\n", $component), $console, Color::GREEN); if (!is_dir($component)) { $console->writeLine(sprintf('[ERROR] Component directory for "%s" does not exist!', $component), Color::WHITE, Color::RED); continue; } chdir($component); $version = $this->tagComponent($component, $minor, $version, $console); chdir($path); $this->emit(sprintf('[DONE] %s tagged at version %s', $component, $version), $console, Color::GREEN); } $console->writeLine(''); $console->writeLine('[DONE] Please verify tags and push the following tag:', Color::GREEN); $console->writeLine(' release-' . $version); return 0; }
/** * @param \ZF\Console\Route $route * @param ConsoleWriter $consoleWriter * @return Environment */ protected function loadEnvironment(Route $route, ConsoleWriter $consoleWriter) { $configPath = $route->getMatchedParam('config-file', getcwd() . DIRECTORY_SEPARATOR . 'processing.config.php'); $additionalConfig = $route->getMatchedParam('config', json_encode([])); $additionalConfig = json_decode($additionalConfig, true); if (is_null($additionalConfig)) { $consoleWriter->writeError("Provided config is not a valid json string"); $consoleWriter->writeError(json_last_error_msg()); return self::MESSAGE_PROCESSING_FAILED; } if (file_exists($configPath)) { $config = (include $configPath); $consoleWriter->writeInfo('Config loaded from ' . $configPath); } elseif (file_exists($configPath . '.dist')) { $config = (include $configPath . '.dist'); $consoleWriter->writeInfo('Config loaded from ' . $configPath); } else { $consoleWriter->writeInfo('No config file specified.'); if (empty($additionalConfig)) { $consoleWriter->writeInfo('Falling back to default config'); } else { $consoleWriter->writeInfo('Using config from argument'); } return $additionalConfig; } $config = ArrayUtils::merge($config, $additionalConfig); $env = Environment::setUp($config); $env->getEventStore()->getActionEventDispatcher()->attachListenerAggregate(new PersistedEventsConsoleWriter($consoleWriter)); return $env; }
/** * Start command processing. * * @param Route $route * @param Console $console * * @return mixed */ public function __invoke(Route $route, Console $console) { $this->route = $route; $this->console = $console; $this->path = realpath($this->route->getMatchedParam('path', getcwd())); if (!is_dir($this->path)) { $this->wlError('Invalid directory provided!'); exit(2); } $this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path); return $this->processCommand(); }
/** * @param Route $route * @param Console $console * @return int */ public function __invoke(Route $route, Console $console) { $path = $route->getMatchedParam('path', realpath(getcwd())); $installPath = $this->createComponentInstallerDirectory($path); if (false === $installPath) { $console->writeLine(sprintf('Unable to create component-installer directory in selected path (%s); aborting', $path), Color::RED); return 1; } $copied = copy(realpath(__DIR__ . '/../ComponentInstaller.php'), sprintf('%s/ComponentInstaller.php', $installPath)); if (false === $copied) { $console->writeLine(sprintf('Unable to copy ComponentInstaller.php to %s/component-installer/; aborting', $path), Color::RED); return 1; } $composer = $this->getComposer($path); if (false === $composer || empty($composer)) { $console->writeLine(sprintf('Unable to read/parse %s/composer.json; aborting', $path), Color::RED); return 1; } $composer = $this->injectAutoloadEntry($composer); $composer = $this->injectScripts($composer); if (false === $this->writeComposer($composer, $path)) { $console->writeLine(sprintf('Unable to write updated %s/composer.json; aborting', $path), Color::RED); return 1; } $console->writeLine('ComponentInstaller installed!', Color::GREEN); return 0; }
public function __invoke(Route $route, Console $console) : int { $console->writeLine('Aggregating feed data...', Color::GREEN); file_put_contents($this->generateFilename($route->getMatchedParam('path')), $this->generateContent($this->getEntries($console))); $console->writeLine('[DONE]', Color::GREEN); return 0; }
public function __invoke(Route $route, Console $console) : int { $basePath = $route->getMatchedParam('path'); $path = realpath($basePath) . '/data/blog'; $cache = realpath($basePath) . '/data/cache/posts'; $baseUri = new Uri('https://mwop.net'); $middleware = $this->blogMiddleware; $console->writeLine('Generating static cache for blog posts', Color::GREEN); // Prepare final handler for middleware $failed = false; $done = function ($req, $res, $err = null) use(&$failed) { $failed = $err ? true : false; }; $parser = new Parser(null, new CommonMarkParser()); foreach (new MarkdownFileFilter($path) as $fileInfo) { $document = $parser->parse(file_get_contents($fileInfo->getPathname())); $metadata = $document->getYAML(); $message = ' ' . $metadata['id']; $length = strlen($message); $width = $console->getWidth(); $console->write($message, Color::BLUE); $canonical = $baseUri->withPath(sprintf('/blog/%s.html', $metadata['id'])); $request = (new Request(new PsrRequest([], [], $canonical, 'GET')))->withUri($canonical)->withAttribute('id', $metadata['id']); $failed = false; $response = $middleware($request, new Response(), $done); if (!$failed) { $this->cacheResponse($metadata['id'], $cache, $response->getBody()); } $this->reportComplete($console, $width, $length, !$failed); } $console->writeLine('ALL DONE', Color::GREEN); return 0; }
/** * @param Route $route */ protected function setAdjectivesFromRoute(Route $route) { $adjectives = $route->getMatchedParam('adjectives'); if (!empty($adjectives)) { Haikunator::$ADJECTIVES = $this->getValuesFromFileIfExists($adjectives); } }
/** * Handle the incoming console request */ public function __invoke(Route $route, Console $console) : int { if (!$route->matchedParam('output')) { return $this->reportError($console, $width, $length, 'Missing output file'); } $message = 'Retrieving Github activity links'; $length = strlen($message); $width = $console->getWidth(); $console->write($message, Color::BLUE); try { $data = $this->reader->read(); } catch (Throwable $e) { return $this->reportError($console, $width, $length, $e); } file_put_contents($route->getMatchedParam('output'), $this->createContentFromData($data, $route->getMatchedParam('template', $this->outputTemplateString))); return $this->reportSuccess($console, $width, $length); }
public function generateDump(Route $route, AdapterInterface $console) { $configFile = $route->getMatchedParam('config'); $outputFile = $route->getMatchedParam('output_file'); $strip = (bool) $route->getMatchedParam('strip'); $console->writeLine("Generating class cache from {$configFile} into {$outputFile}"); if (!file_exists($configFile)) { throw new RuntimeException("Configuration file does not exist: {$configFile}"); } $classes = (include $configFile); if (!is_array($classes)) { throw new RuntimeException("Configuration file does not contain array of class names"); } if (!file_exists(dirname($outputFile))) { mkdir(dirname($outputFile), 0777, true); } $dumper = new ClassDumper(); $cache = $dumper->dump($classes, $strip); file_put_contents($outputFile, "<?php\n" . $cache); }
public function __invoke(Route $route, Console $console) : int { $this->console = $console; $outputDir = $route->getMatchedParam('outputDir'); $baseUri = $route->getMatchedParam('baseUri'); $this->console->writeLine('Generating base feeds'); $this->generateFeeds($outputDir . '/', $baseUri, 'Blog entries :: phly, boy, phly', 'blog', 'blog.feed', [], $this->mapper->fetchAll()); $cloud = $this->mapper->fetchTagCloud(); $tags = array_map(function ($item) { return $item->getTitle(); }, iterator_to_array($cloud->getItemList())); foreach ($tags as $tag) { if (empty($tag)) { continue; } $this->console->writeLine('Generating feeds for tag ' . $tag); $this->generateFeeds(sprintf('%s/%s.', $outputDir, $tag), $baseUri, sprintf('Tag: %s :: phly, boy, phly', $tag), 'blog.tag', 'blog.tag.feed', ['tag' => $tag], $this->mapper->fetchAllByTag($tag)); } return 0; }
public function __invoke(Route $route, Console $console) : int { $serviceWorker = $route->getMatchedParam('serviceWorker'); $console->writeLine('Updating service worker default offline pages'); $paths = $this->defaultPaths; foreach ($this->generatePaths() as $path) { $paths[] = $path; } $this->updateServiceWorker($serviceWorker, $paths); $console->writeLine('[DONE]'); return 0; }
public function __invoke(Route $route, Console $console) : int { $path = $route->getMatchedParam('path'); $console->writeLine('Enabling dist templates... ', Color::BLUE); foreach ($this->distFileMap as $source => $target) { $source = sprintf('%s/%s', $path, $source); $target = sprintf('%s/%s', $path, $target); copy($source, $target); } $console->write('[DONE] ', Color::GREEN); $console->writeLine('Enabling dist templates'); return 0; }
public function __invoke(Route $route, Console $console) : int { $message = 'Creating tag cloud'; $length = strlen($message); $width = $console->getWidth(); $console->write($message, Color::BLUE); if (!$route->matchedParam('output')) { return $this->reportError($console, $width, $length, 'Missing output file'); } $output = $route->getMatchedParam('output'); $cloud = $this->mapper->fetchTagCloud(); $markup = sprintf("<h4>Tag Cloud</h4>\n<div class=\"cloud\">%s</div>", $cloud->render()); file_put_contents($output, $markup); return $this->reportSuccess($console, $width, $length); }
public function __invoke(Route $route, Console $console) : int { $console->write('Clearing static cache... '); $rdi = new RecursiveDirectoryIterator(sprintf(self::PATH_TEMPLATE, $route->getMatchedParam('path'))); $rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::CHILD_FIRST | RecursiveIteratorIterator::LEAVES_ONLY); foreach ($rii as $file) { if (!$file instanceof SplFileInfo || $file->isDir()) { continue; } if ($file->getFilename() === '.placeholder') { continue; } unlink($file->getRealPath()); } $console->writeLine('[DONE]'); return 0; }
public function __invoke(Route $route, Console $console) : int { $basePath = $route->getMatchedParam('path'); $path = realpath($basePath) . '/data/blog'; $console->writeLine('Generating search metadata', Color::BLUE); $documents = []; $parser = new Parser(); foreach (new MarkdownFileFilter($path) as $fileInfo) { $document = $parser->parse(file_get_contents($fileInfo->getPathname()), false); $metadata = $document->getYAML(); $content = $document->getContent(); $documents[] = ['id' => sprintf('/blog/%s.html', $metadata['id']), 'tags' => implode(' ', $metadata['tags']), 'title' => $metadata['title'], 'content' => $content]; } file_put_contents(realpath($basePath) . '/public/js/search_terms.json', json_encode(['docs' => $documents], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); $console->write('[DONE]', Color::GREEN); $console->writeLine(' Generating search metadata'); return 0; }
/** * @param Route $route * @param Console $console * @return int */ public function __invoke(Route $route, Console $console) { $command = $route->getMatchedParam('command', null); $this->application->showUsageMessage($command); return 0; }
public static function run(Route $route, AdapterInterface $console) { $name = $route->getMatchedParam("name", "@gianarb"); $console->writeLine("Hi {$name}, you have call me. Now this is an awesome day!"); }
public function testGetMatchedParamReturnsDefaultValueIfParameterIsNotMatched() { $route = new Route('foo', 'foo [<bar>]'); $matches = $route->match(['foo']); $this->assertNull($route->getMatchedParam('bar')); }
public function deriveVerbosityLevelFrom(Route $route) { $this->verbose = (bool) $route->getMatchedParam('verbose', false); $this->quit = (bool) $route->getMatchedParam('quit', false); }