1: <?php
2: /**
3: * @author Jefferson González
4: * @license MIT
5: * @link http://github.com/peg-org/peg-src Source code.
6: */
7:
8: namespace Peg\Lib;
9:
10: /**
11: * Holds global options and objects.
12: */
13: class Application
14: {
15:
16: /**
17: * Reference to the definition's symbols object.
18: * @var \Peg\Lib\Definitions\Symbols
19: */
20: private static $symbols;
21:
22: // Disable constructor
23: private function __construct(){}
24:
25: /**
26: * Initialize all variables used by Peg. Has to be called before any usage
27: * of peg.
28: */
29: public static function Initialize()
30: {
31: // Initialize the plugin loader and try to load any plugins.
32: self::$plugin_loader = new Plugins\Loader();
33:
34: if(self::ValidExtension())
35: {
36: self::$plugin_loader->Start(self::GetCwd() . "/plugins");
37:
38: if(file_exists(self::GetCwd() . "/peg.conf"))
39: {
40: Settings::SetBackEnd(new Config\INI);
41: Settings::Load(self::GetCwd(), "peg.conf");
42: }
43: else
44: {
45: Settings::SetBackEnd(new Config\JSON);
46: Settings::Load(self::GetCwd(), "peg.json");
47: }
48: }
49: }
50:
51: /**
52: * Check if the current directory is of a valid extension.
53: * @return boolean
54: */
55: public static function ValidExtension()
56: {
57: $dir = self::GetCwd();
58:
59: if(
60: // Templates
61: is_dir($dir . "/templates") &&
62:
63: // Peg configuration file
64: (file_exists($dir . "/peg.conf") || file_exists($dir . "/peg.json"))
65: )
66: {
67: return true;
68: }
69:
70: return false;
71: }
72:
73: /**
74: * Gets the current working directory.
75: * @return string
76: */
77: public static function GetCwd()
78: {
79: return $_SERVER["PWD"];
80: }
81:
82: /**
83: * Retreieve the skeleton path from PEG_SKELETON_PATH or throws
84: * an exception if not exists.
85: * @return string
86: * @throws Exception
87: */
88: public static function GetSkeletonPath()
89: {
90: if(file_exists(PEG_SKELETON_PATH))
91: return PEG_SKELETON_PATH;
92:
93: throw new Exception("Skeleton path not found.");
94: }
95:
96: /**
97: * Loads definitions files if not yet loaded and returns a reference to a
98: * symbols object that can be used throught the application.
99: * @return \Peg\Lib\Definitions\Symbols
100: */
101: public static function &GetDefinitions()
102: {
103: if(!self::ValidExtension())
104: {
105: CommandLine\Error::Show(t("Invalid extension directory, definitions could not be loaded."));
106: }
107:
108: if(!is_object(self::$symbols))
109: {
110: self::$symbols = new Definitions\Symbols;
111:
112: $importer = new Definitions\Importer(
113: self::$symbols,
114: "definitions",
115: Definitions\Type::JSON
116: );
117:
118: $importer->Start();
119: }
120:
121: return self::$symbols;
122: }
123:
124: /**
125: * Get reference to the plugin loader currently used by peg.
126: * @return \Peg\Lib\Plugins\Loader
127: */
128: public static function &GetPluginLoader()
129: {
130: return self::$plugin_loader;
131: }
132: }
133: