Overview

Namespaces

  • None
  • Peg
    • Cli
      • Commands
    • Config
    • Custom
      • Command
        • Action
          • Generate
          • Parse
      • CommandLine
      • Config
      • Localization
      • Utilities
    • Lib
      • Definitions
        • Element
      • Generator
      • Lexers
      • Plugins
      • Signals
        • Data
          • Definitions
          • Lexers
        • Type
  • PHP

Classes

  • Help
  • Init
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: /**
  3:  * @author Jefferson González
  4:  * @license MIT
  5:  * @link http://github.com/peg-org/peg-custom Source code.
  6:  */
  7: 
  8: namespace Peg\Custom\Command\Action;
  9: 
 10: use Peg\Custom\Settings;
 11: use Peg\Custom\Application;
 12: use Peg\Custom\CommandLine\Error;
 13: use Peg\Custom\Utilities\FileSystem;
 14: 
 15: /**
 16:  * Action taken if the init command was executed.
 17:  */
 18: class Init extends \Peg\Custom\CommandLine\Action
 19: {
 20: 
 21:     public function OnCall(\Peg\Custom\CommandLine\Command $command)
 22:     {
 23:         // Get option values
 24:         $authors = $command->GetOption("authors")->GetValue();
 25:         $contributors = $command->GetOption("contributors")->GetValue();
 26:         $name = $command->GetOption("name")->GetValue();
 27:         $version = $command->GetOption("initial-version")->GetValue();
 28:         $force = $command->GetOption("force")->active;
 29:         $config_type = $command->GetOption("config-type")->GetValue();
 30: 
 31:         // Set output directory
 32:         $extension_dir = Application::GetCwd();
 33: 
 34:         if(strlen($command->value) > 0)
 35:             $extension_dir .= "/" . $command->value;
 36: 
 37:         if(!file_exists($extension_dir))
 38:             FileSystem::MakeDir($extension_dir, 0755, true);
 39: 
 40:         // Get extension name
 41:         $dir_parts = explode("/", str_replace("\\", "/", $extension_dir));
 42: 
 43:         $extension = $dir_parts[count($dir_parts) - 1];
 44: 
 45:         if(strlen($name) > 0)
 46:             $extension = $name;
 47: 
 48:         $files = FileSystem::GetDirContent($extension_dir);
 49: 
 50:         if(!$force)
 51:         {
 52:             if(count($files) > 0)
 53:                 Error::Show(t("The directory you are trying to initialize is not empty."));
 54:         }
 55:         
 56:         // Create configuration file
 57:         if($config_type == "json")
 58:         {
 59:             Settings::SetBackEnd(new \Peg\Custom\Config\JSON);
 60:             Settings::Load($extension_dir, "peg.json");
 61:         }
 62:         else
 63:         {
 64:             Settings::SetBackEnd(new \Peg\Custom\Config\INI);
 65:             Settings::Load($extension_dir, "peg.conf");
 66:         }
 67: 
 68:         $this->CopySkeleton($extension_dir, $extension, $version, $authors, $contributors);
 69:     }
 70: 
 71:     /**
 72:      * Takes the actions needed to generate the extension skeleton.
 73:      * @param string $directory Path to extension
 74:      * @param string $extension Name of the extension
 75:      * @param string $authors Comman sperated list of authors
 76:      * @param string $contributors Comma seperated list of contributors
 77:      */
 78:     private function CopySkeleton($directory, $extension, $version, $authors, $contributors)
 79:     {
 80:         Settings::SetExtensionName($extension);
 81:         Settings::SetAuthors($authors);
 82:         Settings::SetContributors($contributors);
 83:         Settings::SetVersion($version);
 84: 
 85:         FileSystem::RecursiveCopyDir(Application::GetSkeletonPath(), $directory);
 86: 
 87:         // Modify CHANGES template
 88:         $date = date("F d Y");
 89: 
 90:         ob_start();
 91:         include($directory . "/CHANGES");
 92: 
 93:         $changes_content = ob_get_contents();
 94:         ob_end_clean();
 95: 
 96:         file_put_contents($directory . "/CHANGES", $changes_content);
 97: 
 98:         // Modify CREDITS template
 99:         $developers = trim($authors . ", " . $contributors, ", ");
100: 
101:         ob_start();
102:         include($directory . "/CREDITS");
103: 
104:         $credits_content = ob_get_contents();
105:         ob_end_clean();
106: 
107:         file_put_contents($directory . "/CREDITS", $credits_content);
108:     }
109: 
110: }
PEG Api API documentation generated by ApiGen 2.8.0