public function getTextFrom($path) { $path = PathHelper::getAbsolutePath($path, $this->pieCrust->getRootDir()); if (!is_file($path)) { throw new PieCrustException("Invalid path for 'text_from': {$path}"); } return file_get_contents($path); }
public function run(ChefContext $context) { $logger = $context->getLog(); $pieCrust = $context->getApp(); $result = $context->getResult(); // Get some options. $exact = $result->command->options['exact']; $fullPath = $result->command->options['full_path']; // If no type filters are given, return all types. $returnAllTypes = ($result->command->options['pages'] == false and $result->command->options['posts'] == false and $result->command->options['templates'] == false); // Validate the argument. $pattern = $result->command->args['pattern']; if ($exact) { // Check we have a path to match, and get its absolute value. if (!$pattern) { throw new PieCrustException("You need to specify a path when using the `--exact` option."); } $pattern = PathHelper::getAbsolutePath($pattern); } else { // If a pattern was given, get the Regex'd version. if ($pattern) { $pattern = PathHelper::globToRegex($pattern); } } $result->command->args['pattern'] = $pattern; $foundAny = false; // Find pages. if ($returnAllTypes or $result->command->options['pages']) { $pages = PageHelper::getPages($pieCrust); $foundAny |= $this->findPages($context, $pages); } // Find posts. if ($returnAllTypes or $result->command->options['posts']) { $blogKeys = $pieCrust->getConfig()->getValue('site/blogs'); if ($result->command->options['blog']) { $blogKeys = array($result->command->options['blog']); } foreach ($blogKeys as $blogKey) { $pages = PageHelper::getPosts($pieCrust, $blogKey); $pagesIterator = new \ArrayIterator($pages); $sorter = new DateSortIterator($pagesIterator); $pages = iterator_to_array($sorter); $foundAny |= $this->findPages($context, $pages, $blogKey); } } // Find templates. if ($returnAllTypes or $result->command->options['templates']) { $templatesDirs = $pieCrust->getTemplatesDirs(); foreach ($templatesDirs as $dir) { $foundAny |= $this->findTemplates($context, $dir); } } if (!$foundAny) { $pattern = $result->command->args['pattern']; $logger->info("No match found for '{$pattern}'."); } return 0; }
public function run(ChefContext $context) { $logger = $context->getLog(); $pieCrust = $context->getApp(); $result = $context->getResult(); // Get some options. $exact = $result->command->options['exact']; $fullPath = $result->command->options['full_path']; // If no type filters are given, return all types. $returnAllTypes = ($result->command->options['pages'] == false and $result->command->options['posts'] == false and $result->command->options['templates'] == false); // Validate the argument. $pattern = $result->command->args['pattern']; if ($exact) { // Check we have a path to match, and get its absolute value. if (!$pattern) { throw new PieCrustException("You need to specify a path when using the `--exact` option."); } $pattern = PathHelper::getAbsolutePath($pattern); } else { // If a pattern was given, get the Regex'd version. if ($pattern) { $pattern = PathHelper::globToRegex($pattern); } } // Get the pages and posts. $pages = array(); if ($returnAllTypes or $result->command->options['pages']) { $pages = PageHelper::getPages($pieCrust); } if ($returnAllTypes or $result->command->options['posts']) { $blogKeys = $pieCrust->getConfig()->getValue('site/blogs'); if ($result->command->options['blog']) { $blogKeys = array($result->command->options['blog']); } foreach ($blogKeys as $blogKey) { $pages = array_merge($pages, PageHelper::getPosts($pieCrust, $blogKey)); } } // Get some other stuff. $returnComponents = $result->command->options['page_components']; // Get a regex for the posts file-naming convention. $fs = FileSystem::create($pieCrust); $pathComponentsRegex = preg_quote($fs->getPostPathFormat(), '/'); $pathComponentsRegex = str_replace(array('%year%', '%month%', '%day%', '%slug%'), array('(\\d{4})', '(\\d{2})', '(\\d{2})', '(.+)'), $pathComponentsRegex); $pathComponentsRegex = '/' . $pathComponentsRegex . '/'; // Print the matching pages. foreach ($pages as $page) { if ($result->command->options['no_special']) { // Skip special pages. if ($page->getUri() == PieCrustDefaults::CATEGORY_PAGE_NAME or $page->getUri() == PieCrustDefaults::TAG_PAGE_NAME) { continue; } } if ($exact) { // Match the path exactly, or pass. if (str_replace('\\', '/', $pattern) != str_replace('\\', '/', $page->getPath())) { continue; } } else { if ($pattern) { // Match the regex, or pass. if (!preg_match($pattern, $page->getUri())) { continue; } } } $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $page->getPath()); if (!$fullPath) { $path = PieCrustHelper::getRelativePath($pieCrust, $path); } if ($returnComponents) { $components = array('path' => $path, 'type' => 'page', 'uri' => $page->getUri(), 'slug' => $page->getUri()); if (PageHelper::isPost($page)) { $matches = array(); if (preg_match($pathComponentsRegex, str_replace('\\', '/', $path), $matches) !== 1) { throw new PieCrustException("Can't extract path components from path: {$path}"); } $components['type'] = 'post'; $components['year'] = $matches[1]; $components['month'] = $matches[2]; $components['day'] = $matches[3]; $components['slug'] = $matches[4]; } $str = ''; foreach ($components as $k => $v) { $str .= $k . ': ' . $v . PHP_EOL; } $logger->info($str); } else { $logger->info($path); } } // Get the template files and print them. if ($returnAllTypes or $result->command->options['templates']) { $templatesDirs = $pieCrust->getTemplatesDirs(); foreach ($templatesDirs as $dir) { $dirIt = new \RecursiveDirectoryIterator($dir); $it = new \RecursiveIteratorIterator($dirIt); foreach ($it as $path) { if ($it->isDot()) { continue; } $relativePath = PieCrustHelper::getRelativePath($pieCrust, $path->getPathname()); if ($exact) { // Match the path exactly, or pass. if (str_replace('\\', '/', $pattern) != str_replace('\\', '/', $path->getPathname())) { continue; } } else { if ($pattern) { // Match the regex, or pass. if (!preg_match($pattern, $relativePath)) { continue; } } } // Get the path to print. $finalPath = $relativePath; if ($fullPath) { $finalPath = $path->getPathname(); } $finalPath = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $finalPath); // Print the information! if ($returnComponents) { $logger->info("path: {$finalPath}"); $logger->info("type: template"); } else { $logger->info($finalPath); } } } } return 0; }
/** * Adds a directory that contains some user plugins. */ public function addPluginsDir($dir) { $this->getPluginsDirs(); // Ensure defaults are created. if (!is_array($dir)) { $dir = array($dir); } foreach ($dir as $d) { $absolute = PathHelper::getAbsolutePath($d, $this->getRootDir()); if (is_dir($absolute) === false) { throw new PieCrustException("The specified plugins directory doesn't exist: " . $absolute); } $this->pluginsDirs[] = rtrim($absolute, '/\\') . '/'; } }
/** * Runs Chef given some command-line arguments. */ public function runUnsafe($userArgc = null, $userArgv = null) { // Get the arguments. if ($userArgc == null || $userArgv == null) { $getopt = new \Console_Getopt(); $userArgv = $getopt->readPHPArgv(); // `readPHPArgv` returns a `PEAR_Error` (or something like it) if // it can't figure out the CLI arguments. if (!is_array($userArgv)) { throw new PieCrustException($userArgv->getMessage()); } $userArgc = count($userArgv); } // Find whether the '--root' parameter was given. $rootDir = null; foreach ($userArgv as $arg) { if (substr($arg, 0, strlen('--root=')) == '--root=') { $rootDir = substr($arg, strlen('--root=')); break; } } if ($rootDir == null) { // No root given. Find it ourselves. $rootDir = PathHelper::getAppRootDir(getcwd()); } else { // The root was given. $rootDir = PathHelper::getAbsolutePath($rootDir); if (!is_dir($rootDir)) { throw new PieCrustException("The given root directory doesn't exist: " . $rootDir); } } // Build the appropriate app. if ($rootDir == null) { $pieCrust = new NullPieCrust(); } else { $pieCrust = new PieCrust(array('root' => $rootDir, 'cache' => !in_array('--no-cache', $userArgv))); } // Set up the command line parser. $parser = new \Console_CommandLine(array('name' => 'chef', 'description' => 'The PieCrust chef manages your website.', 'version' => PieCrustDefaults::VERSION)); // Sort commands by name. $sortedCommands = $pieCrust->getPluginLoader()->getCommands(); usort($sortedCommands, function ($c1, $c2) { return strcmp($c1->getName(), $c2->getName()); }); // Add commands to the parser. foreach ($sortedCommands as $command) { $commandParser = $parser->addCommand($command->getName()); $command->setupParser($commandParser, $pieCrust); $this->addCommonOptionsAndArguments($commandParser); } // Parse the command line. try { $result = $parser->parse($userArgc, $userArgv); } catch (Exception $e) { $parser->displayError($e->getMessage()); return 1; } // If no command was given, use `help`. if (empty($result->command_name)) { $result = $parser->parse(2, array('chef', 'help')); } // Create the log. $debugMode = $result->command->options['debug']; $quietMode = $result->command->options['quiet']; if ($debugMode && $quietMode) { $parser->displayError("You can't specify both --debug and --quiet."); return 1; } $log = \Log::singleton('console', 'Chef', '', array('lineFormat' => '%{message}')); // Make the log available for debugging purposes. $GLOBALS['__CHEF_LOG'] = $log; // Handle deprecated stuff. if ($result->command->options['no_cache_old']) { $log->warning("The `--nocache` option has been renamed `--no-cache`."); $result->command->options['no_cache'] = true; } // Run the command. foreach ($pieCrust->getPluginLoader()->getCommands() as $command) { if ($command->getName() == $result->command_name) { try { if ($rootDir == null && $command->requiresWebsite()) { $cwd = getcwd(); throw new PieCrustException("No PieCrust website in '{$cwd}' ('_content/config.yml' not found!)."); } $context = new ChefContext($pieCrust, $result, $log); $context->setVerbosity($debugMode ? 'debug' : ($quietMode ? 'quiet' : 'default')); $command->run($context); return; } catch (Exception $e) { $log->emerg(self::getErrorMessage($e, $debugMode)); return 1; } } } }