public function testOutsideAppRootDir()
 {
     $structure = array('blah' => array('web' => array('_content' => array('config.yml' => 'dummy')), 'other' => array()));
     $root = vfsStream::setup('root', null, $structure);
     $cwd = vfsStream::url('root');
     $webRoot = PathHelper::getAppRootDir($cwd);
     $this->assertNull($webRoot);
     $cwd = vfsStream::url('root/other');
     $webRoot = PathHelper::getAppRootDir($cwd);
     $this->assertNull($webRoot);
 }
Beispiel #2
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;
             }
         }
     }
 }
Beispiel #3
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;
             }
         }
     }
 }