public function runAppInit($cleanCache = false)
 {
     if ($cleanCache) {
         ensure_cache(PIECRUST_BENCHMARKS_CACHE_DIR, true);
     }
     $pc = new PieCrust(array('cache' => (bool) $this->cacheDir, 'root' => $this->rootDir));
     $pc->setCacheDir($this->cacheDir);
     $pc->getConfig();
     return $pc;
 }
    /**
     * @dataProvider urlFormatsDataProvider
     */
    public function testUrlFormats($prettyUrls, $trailingSlash, $expectedContents)
    {
        $fs = MockFileSystem::create();
        $fs->withPage('test_page', array('layout' => 'none', 'format' => 'none'), <<<'EOD'
Normal: {{pcurl('normal')}}
Normal in folder: {{pcurl('somewhere/normal')}}
Ext: {{pcurl('foo.ext')}}
Ext in folder: {{pcurl('somewhere/foo.ext')}}
EOD
);
        $fs->withPage('other_page.foo', array('layout' => 'none', 'format' => 'none'), "THIS IS FOO!");
        $app = new PieCrust(array('cache' => false, 'root' => $fs->url('kitchen')));
        $app->getConfig()->setValue('site/pretty_urls', $prettyUrls);
        if ($trailingSlash) {
            $app->getConfig()->setValue('baker/trailing_slash', true);
        }
        $baker = new PieCrustBaker($app);
        $baker->bake();
        $otherPagePath = 'other_page.foo';
        if ($prettyUrls) {
            $otherPagePath = 'other_page.foo/index.html';
        }
        $this->assertFileExists($fs->url('kitchen/_counter/' . $otherPagePath));
        $this->assertEquals("THIS IS FOO!", file_get_contents($fs->url('kitchen/_counter/' . $otherPagePath)));
        $fileName = $prettyUrls ? 'test_page/index.html' : 'test_page.html';
        $this->assertFileExists($fs->url('kitchen/_counter/' . $fileName));
        $this->assertEquals($expectedContents, file_get_contents($fs->url('kitchen/_counter/' . $fileName)));
    }
Пример #3
0
 protected function prebake($server = null, $path = null)
 {
     // Things like the plugin loader will add paths to the PHP include path.
     // Let's save it and restore it later.
     $includePath = get_include_path();
     $pieCrust = new PieCrust(array('root' => $this->rootDir, 'cache' => $this->options['cache']), $server);
     $parameters = $pieCrust->getConfig()->getValue('baker');
     if ($parameters == null) {
         $parameters = array();
     }
     $parameters = array_merge(array('smart' => true, 'mounts' => array(), 'processors' => '*', 'skip_patterns' => array(), 'force_patterns' => array()), $parameters);
     $dirBaker = new DirectoryBaker($pieCrust, $this->bakeCacheDir, array('smart' => $parameters['smart'], 'mounts' => $parameters['mounts'], 'processors' => $parameters['processors'], 'skip_patterns' => $parameters['skip_patterns'], 'force_patterns' => $parameters['force_patterns']), $this->logger);
     $dirBaker->bake($path);
     // Restore the include path.
     set_include_path($includePath);
     return $dirBaker->getBakedFiles();
 }
Пример #4
0
 /**
  * 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 if whether the `--root` or `--config` parameters were given.
     $rootDir = null;
     $isThemeSite = false;
     $configVariant = null;
     for ($i = 1; $i < count($userArgv); ++$i) {
         $arg = $userArgv[$i];
         if (substr($arg, 0, strlen('--root=')) == '--root=') {
             $rootDir = substr($arg, strlen('--root='));
             if (substr($rootDir, 0, 1) == '~') {
                 $rootDir = getenv("HOME") . substr($rootDir, 1);
             }
         } elseif ($arg == '--root') {
             $rootDir = $userArgv[$i + 1];
             ++$i;
         } elseif (substr($arg, 0, strlen('--config=')) == '--config=') {
             $configVariant = substr($arg, strlen('--config='));
         } elseif ($arg == '--config') {
             $configVariant = $userArgv[$i + 1];
             ++$i;
         } elseif ($arg == '--theme') {
             $isThemeSite = true;
         } else {
             if ($arg[0] != '-') {
                 // End of the global arguments sections. This is
                 // the command name.
                 break;
             }
         }
     }
     if ($rootDir == null) {
         // No root given. Find it ourselves.
         $rootDir = PathHelper::getAppRootDir(getcwd(), $isThemeSite);
     } else {
         // The root was given.
         $rootDir = trim($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 {
         $environment = new ChefEnvironment();
         $pieCrust = new PieCrust(array('root' => $rootDir, 'cache' => !in_array('--no-cache', $userArgv), 'environment' => $environment, 'theme_site' => $isThemeSite));
     }
     // Pre-load the correct config variant if any was specified.
     if ($configVariant != null) {
         // You can't apply a config variant if there's no website.
         if ($rootDir == null) {
             $cwd = getcwd();
             throw new PieCrustException("No PieCrust website in '{$cwd}' ('_content/config.yml' not found!).");
         }
         $configVariant = trim($configVariant, " \"");
         $pieCrust->getConfig()->applyVariant('variants/' . $configVariant);
     }
     // Set up the command line parser.
     $parser = new \Console_CommandLine(array('name' => 'chef', 'description' => 'The PieCrust chef manages your website.', 'version' => PieCrustDefaults::VERSION));
     $parser->renderer = new ChefCommandLineRenderer($parser);
     $this->addCommonOptionsAndArguments($parser);
     // Sort commands by name.
     $sortedCommands = $pieCrust->getPluginLoader()->getCommands();
     usort($sortedCommands, function ($com1, $com2) {
         return strcmp($com1->getName(), $com2->getName());
     });
     // Add commands to the parser.
     foreach ($sortedCommands as $command) {
         $commandParser = $parser->addCommand($command->getName());
         $command->setupParser($commandParser, $pieCrust);
     }
     // Parse the command line.
     try {
         $result = $parser->parse($userArgc, $userArgv);
     } catch (Exception $e) {
         $parser->displayError($e->getMessage(), false);
         return 3;
     }
     // If no command was given, use `help`.
     if (empty($result->command_name)) {
         $result = $parser->parse(2, array('chef', 'help'));
     }
     // Create the log.
     $debugMode = $result->options['debug'];
     $quietMode = $result->options['quiet'];
     if ($debugMode && $quietMode) {
         $parser->displayError("You can't specify both --debug and --quiet.", false);
         return 1;
     }
     $log = new ChefLog('Chef');
     // Log to a file.
     if ($result->options['log']) {
         $log->addFileLog($result->options['log']);
     }
     // Make the log available to PieCrust.
     if ($rootDir != null) {
         $environment->setLog($log);
     }
     // Make the log available for debugging purposes.
     $GLOBALS['__CHEF_LOG'] = $log;
     // 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!).");
                 }
                 if ($debugMode) {
                     $log->debug("PieCrust v." . PieCrustDefaults::VERSION);
                     $log->debug("  Website: {$rootDir}");
                 }
                 $context = new ChefContext($pieCrust, $result, $log);
                 $context->setVerbosity($debugMode ? 'debug' : ($quietMode ? 'quiet' : 'default'));
                 $command->run($context);
                 return;
             } catch (Exception $e) {
                 $log->exception($e, $debugMode);
                 return 1;
             }
         }
     }
 }
Пример #5
0
 /**
  * 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;
             }
         }
     }
 }