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;
9:
10: /**
11: * Holds global options and objects.
12: */
13: class Application
14: {
15:
16: /**
17: * Reference to the global parser.
18: * @var \Peg\Custom\CommandLine\Parser
19: */
20: private static $cli_parser;
21:
22: /**
23: * Reference to the plugins loader.
24: * @var \Peg\Lib\Plugins\Loader;
25: */
26: private static $plugin_loader;
27:
28: /**
29: * Reference to the help command.
30: * @var \Peg\Custom\Command\Help
31: */
32: private static $help_command;
33:
34: /**
35: * Reference to the init command.
36: * @var \Peg\Custom\Command\Init
37: */
38: private static $init_command;
39:
40: /**
41: * Reference to the parse command.
42: * @var \Peg\Custom\Command\Parse
43: */
44: private static $parse_command;
45:
46: /**
47: * Reference to the generate command.
48: * @var \Peg\Custom\Command\Generate
49: */
50: private static $generate_command;
51:
52: /**
53: * Reference to the definition's symbols object.
54: * @var \Peg\Lib\Definitions\Symbols
55: */
56: private static $symbols;
57:
58: // Disable constructor
59: private function __construct(){}
60:
61: /**
62: * Initialize all variables used by Peg. Has to be called before any usage
63: * of peg.
64: */
65: public static function Initialize()
66: {
67: self::$cli_parser = new CommandLine\Parser;
68:
69: // Set Application details
70: self::$cli_parser->application_name = "peg-custom";
71: self::$cli_parser->application_version = "1.0";
72: self::$cli_parser->application_description = t("PHP Extension Generator (http://github.com/peg-org/peg-custom)");
73:
74: // Create commands
75: self::$help_command = new Command\Help;
76: self::$init_command = new Command\Init;
77: self::$parse_command = new Command\Parse;
78: self::$generate_command = new Command\Generate;
79:
80: // Register command operations
81: self::$cli_parser->RegisterCommand(self::$help_command);
82: self::$cli_parser->RegisterCommand(self::$init_command);
83: self::$cli_parser->RegisterCommand(self::$parse_command);
84: self::$cli_parser->RegisterCommand(self::$generate_command);
85:
86: // Initialize the plugin loader and try to load any plugins.
87: self::$plugin_loader = new \Peg\Lib\Plugins\Loader();
88:
89: if(self::ValidExtension())
90: {
91: self::$plugin_loader->Start(self::GetCwd() . "/plugins");
92:
93: if(file_exists(self::GetCwd() . "/peg.conf"))
94: {
95: Settings::SetBackEnd(new Config\INI);
96: Settings::Load(self::GetCwd(), "peg.conf");
97: }
98: else
99: {
100: Settings::SetBackEnd(new Config\JSON);
101: Settings::Load(self::GetCwd(), "peg.json");
102: }
103: }
104: }
105:
106: /**
107: * Check if the current directory is of a valid extension.
108: * @return boolean
109: */
110: public static function ValidExtension()
111: {
112: $dir = self::GetCwd();
113:
114: if(
115: // Templates
116: is_dir($dir . "/templates") &&
117:
118: // Peg configuration file
119: (file_exists($dir . "/peg.conf") || file_exists($dir . "/peg.json"))
120: )
121: {
122: return true;
123: }
124:
125: return false;
126: }
127:
128: /**
129: * Gets the current working directory.
130: * @return string
131: */
132: public static function GetCwd()
133: {
134: return $_SERVER["PWD"];
135: }
136:
137: /**
138: * Retreieve the skeleton path from PEG_SKELETON_PATH or throws
139: * an exception if not exists.
140: * @return string
141: * @throws Exception
142: */
143: public static function GetSkeletonPath()
144: {
145: if(file_exists(PEG_SKELETON_PATH))
146: return PEG_SKELETON_PATH;
147:
148: throw new Exception("Skeleton path not found.");
149: }
150:
151: /**
152: * Gets the global command line parser.
153: * @return \Peg\Custom\CommandLine\Parser
154: */
155: public static function &GetCLIParser()
156: {
157: return self::$cli_parser;
158: }
159:
160: /**
161: * Gets a reference to init command currently used by peg.
162: * @return \Peg\Custom\Command\Init
163: */
164: public static function &GetInitCommand()
165: {
166: return self::$init_command;
167: }
168:
169: /**
170: * Gets a reference to help command currently used by peg.
171: * @return \Peg\Custom\Command\Help
172: */
173: public static function &GetHelpCommand()
174: {
175: return self::$help_command;
176: }
177:
178: /**
179: * Gets a reference to parse command currently used by peg.
180: * @return \Peg\Custom\Command\Parse
181: */
182: public static function &GetParseCommand()
183: {
184: return self::$parse_command;
185: }
186:
187: /**
188: * Gets a reference to generate command currently used by peg.
189: * @return \Peg\Custom\Command\Parse
190: */
191: public static function &GetGenerateCommand()
192: {
193: return self::$generate_command;
194: }
195:
196: /**
197: * Loads definitions files if not yet loaded and returns a reference to a
198: * symbols object that can be used throught the application.
199: * @return \Peg\Lib\Definitions\Symbols
200: */
201: public static function &GetDefinitions()
202: {
203: if(!self::ValidExtension())
204: {
205: CommandLine\Error::Show(t("Invalid extension directory, definitions could not be loaded."));
206: }
207:
208: if(!is_object(self::$symbols))
209: {
210: self::$symbols = new Definitions\Symbols;
211:
212: $importer = new Definitions\Importer(
213: self::$symbols,
214: "definitions",
215: Definitions\Type::JSON
216: );
217:
218: $importer->Start();
219: }
220:
221: return self::$symbols;
222: }
223:
224: /**
225: * Get reference to the plugin loader currently used by peg.
226: * @return \Peg\Lib\Plugins\Loader
227: */
228: public static function &GetPluginLoader()
229: {
230: return self::$plugin_loader;
231: }
232: }