1 <?php
2 /**
3 * Contains Globals class for Alphred
4 *
5 * PHP version 5
6 *
7 * @package Alphred
8 * @copyright Shawn Patrick Rice 2014
9 * @license http://opensource.org/licenses/MIT MIT
10 * @version 1.0.0
11 * @author Shawn Patrick Rice <rice@shawnrice.org>
12 * @link http://www.github.com/shawnrice/alphred
13 * @link http://shawnrice.github.io/alphred
14 * @since File available since Release 1.0.0
15 *
16 */
17
18 namespace Alphred;
19
20 /**
21 * A class to reteive certain Global variables
22 *
23 * Basically, it gives you access to the subset of variables.
24 *
25 * This class was written so that CodeClimate would stop throwing a fit because
26 * I was accessing the $_SERVER variable directly.
27 *
28 */
29 class Globals {
30
31 /**
32 * An array of Global variables that can be accessed
33 *
34 * @since 1.0.0
35 *
36 * @var array
37 */
38 private static $variables = [
39 'alfred_theme_background',
40 'alfred_theme_subtext',
41 'alfred_version',
42 'alfred_version_build',
43 'alfred_workflow_bundleid',
44 'alfred_workflow_cache',
45 'alfred_workflow_data',
46 'alfred_workflow_name',
47 'alfred_workflow_uid',
48 'ALPHRED_IN_BACKGROUND', // This is internal for background awareness
49 'PWD',
50 'USER'
51 ];
52
53 /**
54 * Throws an exception if you try to instantiate it
55 *
56 * @throws \Alphred\UseOnlyAsStatic if you try to institate a Globals object
57 */
58 public function __construct() {
59 throw new UseOnlyAsStatic( 'The Globals class is to be used statically only.', 1 );
60 }
61
62 /**
63 * Retrieves a variable from the global $_SERVER array
64 *
65 * @since 1.0.0
66 * @throws \Alphred\RunningOutsideOfAlfred
67 *
68 * @param string $name name of the variable
69 * @return string value of the variable
70 */
71 public static function get( $name ) {
72 // Check if the variable is in the appropriate array
73 if ( in_array( $name, self::$variables ) ) {
74 // If the variable is set, then return it
75 if ( isset( $_SERVER[ $name ] ) ) {
76 return $_SERVER[ $name ];
77 }
78 // Special case for 'running in background': we do the workflow environment check otherwise,
79 // so we'll just return false if not set.
80 if ( 'ALPHRED_IN_BACKGROUND' == $name ) {
81 return false;
82 }
83 // The variable is not set, so we'll throw an exception
84 throw new RunningOutsideOfAlfred( 'The Globals can be accessed only within a workflow environment.', 4 );
85 } else {
86 // Should this be an exception?
87 return false;
88 }
89 }
90
91 /**
92 * Retrieves the bundle id of the workflow from the global $_SERVER array
93 *
94 * @since 1.0.0
95 *
96 * @return string the bundle id of the running workflow
97 */
98 public static function bundle() {
99 return self::get( 'alfred_workflow_bundleid' );
100 }
101
102 /**
103 * Retrieves the data directory of the running workflow
104 *
105 * @since 1.0.0
106 *
107 * @return string path to the workflow's data directory
108 */
109 public static function data() {
110 return self::get( 'alfred_workflow_data' );
111 }
112
113 /**
114 * Retrieves the cache directory of the running workflow
115 *
116 * @since 1.0.0
117 *
118 * @return string path to the workflow's cache directory
119 */
120 public static function cache() {
121 return self::get( 'alfred_workflow_cache' );
122 }
123
124 /**
125 * Checks if the script is running in the background
126 *
127 * This is aware __only__ of a script running in the background if
128 * it was launched by the Alphred wrapper's background() method.
129 *
130 * @since 1.0.0
131 * @see \Alphred::background() To see how to launch a background script.
132 *
133 * @return boolean [description]
134 */
135 public static function is_background() {
136 // This will trigger an exception if running outside of Alfred
137 self::get( 'alfred_workflow_data' );
138 if ( self::get( 'ALPHRED_IN_BACKGROUND' ) ) {
139 return true;
140 }
141 // The variable is not set, but we're running inside of a workflow
142 // environment, so that mean we aren't in the background, so return
143 // false
144 return false;
145 }
146
147 }