PhpToolCase
Api Documentation Version 0.9.2
ptc-helpers.php
Go to the documentation of this file.
1 <?php
2 
3  /**
4  * PHPTOOLCASE HELPER FUNCTIONS FILE
5  * <br>All helper functions work only if in the same namespace of the library,
6  * with the exception of the PtcDebug class which is only loaded once.
7  * PHP version 5.3
8  * @category Library
9  * @version 0.9.2
10  * @author Irony <carlo@salapc.com>
11  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
12  * @link http://phptoolcase.com
13  */
14 
15 
16  ### DEBUGGER & LOGGER COMPONENT HELPERS #############################
17 
18  /**
19  * Writes data to the messages panel. See PtcDebug::bufferLog( )
20  * @param mixed $string the string to pass
21  * @param mixed $statement some statement if required
22  * @param string $category a category for the messages panel
23  */
24  function ptc_log( $string , $statement = null , $category = null )
25  {
26  return ptc_run( 'bufferLog' , @_PTCDEBUG_NAMESPACE_ , array( $string , $statement , $category ) );
27  }
28  /**
29  * Writes data to the sql panel. See PtcDebug::bufferSql()
30  * @param mixed $string the string to pass
31  * @param mixed $statement some statement if required
32  * @param string $category a category for the sql panel
33  */
34  function ptc_log_sql( $string , $statement = null , $category = null )
35  {
36  return ptc_run( 'bufferSql' , @_PTCDEBUG_NAMESPACE_ , array( $string , $statement , $category ) );
37  }
38  /**
39  * Monitors the execution of php code, or sql queries based on a reference. See PtcDebug::stopTimer( )
40  * @param string $reference a reference to look for ("$statement")
41  * @param string|numeric $precision sec/ms
42  */
43  function ptc_stop_timer( $reference = null , $precision = 1 )
44  {
45  return ptc_run( 'stopTimer' , @_PTCDEBUG_NAMESPACE_ , array( $reference , $precision ) );
46  }
47  /**
48  * Attaches data to the buffer array based on a reference. See PtcDebug::addToBuffer( )
49  * @param string $reference a reference to look for ("$statement")
50  * @param mixed $string the message to show
51  * @param string $statement a new statement if required
52  */
53  function ptc_attach( $reference , $string , $statement = null )
54  {
55  return ptc_run( 'addToBuffer' , @_PTCDEBUG_NAMESPACE_ , array( $reference , $string , $statement ) );
56  }
57  /**
58  * Watches a variable that is in a declare(ticks=n){ code block }, for changes. See PtcDebug::watch( )
59  * @param string $variableName the name of the variable to watch
60  * @param string $callback a callback to retrieve the variable
61  */
62  function ptc_watch( $variableName , $callback = null )
63  {
64  return ptc_run( 'watch' , @_PTCDEBUG_NAMESPACE_ , array( $variableName , $callback ) );
65  }
66  /**
67  * Starts the code coverage analysis utility to find executed lines. See PtcDebug::startCoverage( )
68  */
69  function ptc_start_coverage( ) { return ptc_run( 'startCoverage' , @_PTCDEBUG_NAMESPACE_ ); }
70  /**
71  * Stop the code coverage analysis utility. See PtcDebug::stopCoverage( )
72  */
73  function ptc_stop_coverage( ) { return ptc_run( 'stopCoverage' ,@_PTCDEBUG_NAMESPACE_ ); }
74  /**
75  * Starts the function calls trace utility. See PtcDebug::startTrace( )
76  */
77  function ptc_start_trace( ) { return ptc_run( 'startTrace' , @_PTCDEBUG_NAMESPACE_ ); }
78  /**
79  * Stop the function calls trace utility. See PtcDebug::stopTrace( )
80  */
81  function ptc_stop_trace( ) { return ptc_run( 'stopTrace' ,@_PTCDEBUG_NAMESPACE_ ); }
82 
83 
84  ### HANDYMAN COMPONENT HELPERS ###############################
85 
86  /**
87  * Retrieves the application paths stored in the PtcHandyMan::$_appPaths array. See PtcHandyMan::getAppPath( )
88  * @param string $type the path type
89  */
90  function ptc_path( $type = null )
91  {
92  $handyman = '_PTCHANDYMAN_' . @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
93  return ptc_run( 'getAppPaths' , @constant( $handyman ) , array( $type ) );
94  }
95  /**
96  * Adds application paths to the PtcHandyMan::$_appPaths array. See PtcHandyMan::addAppPath( )
97  * @param array | string $paths the application paths to add
98  */
99  function ptc_add_path( $paths )
100  {
101  $handyman = '_PTCHANDYMAN_' . @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
102  return ptc_run( 'addAppPath' , @constant( $handyman ) , array( $paths ) );
103  }
104  /**
105  * Retrieves the directories the autoloader uses to load classes. See PtcHandyMan::getDirs( )
106  * @param string $type the directory type
107  */
108  function ptc_dir( $type = null )
109  {
110  $handyman = '_PTCHANDYMAN_' . @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
111  return ptc_run( 'getDirs' , @constant( $handyman ) , array( $type ) );
112  }
113  /**
114  * Adds directories to the autoloader to load classes. See PtcHandyMan::addDirs( )
115  * @param array|string $directories the full path to the directories holding the classes
116  */
117  function ptc_add_dir( $directories )
118  {
119  $handyman = '_PTCHANDYMAN_' . @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
120  return ptc_run( 'addDirs' , @constant( $handyman ) , array( $directories ) );
121  }
122  /**
123  * Adds files to the class autoloader. See PtcHandyMan::addFiles( )
124  * @param array $files the full path to the class file(s)
125  */
126  function ptc_add_file( $files )
127  {
128  $handyman = '_PTCHANDYMAN_' . @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
129  return ptc_run( 'addFiles' , @constant( $handyman ) , array( $files ) );
130  }
131  /**
132  * Gets protected and private properties. See PtcHandyMan::getProperty( )
133  * @param mixed $class the name or the initialized class object
134  * @param string $propertyName the name of the property
135  */
136  function ptc_get_prop( $class , $propertyName )
137  {
138  $handyman = '_PTCHANDYMAN_' . @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
139  return ptc_run( 'getProperty' , @constant( $handyman ) , array( $class , $propertyName ) );
140  }
141 
142  ### EVENT DISPATCHER COMPONENT HELPERS ##################################
143 
144  /**
145  * Adds a listener to an event. See PtcEvent::listen( )
146  * @param string $event the event name, example: "event.sub_event"
147  * @param mixed $callback a valid callback ( closure , function , class )
148  * @param numeric $priority a numeric value, higher values will execute first
149  */
150  function ptc_listen( $event , $callback , $priority = 0 )
151  {
152  $ptc_event = '_PTCEVENT_' . @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
153  return ptc_run( 'listen' , @constant( $ptc_event ) , array( $event , $callback , $priority ) );
154  }
155  /**
156  * Fires an event See PtcEvent::fire( )
157  * @param string $event the event name to fire
158  * @param array $data an array with the data you wish to pass to the listeners
159  */
160  function ptc_fire( $event , $data )
161  {
162  $ptc_event = '_PTCEVENT_' . @strtoupper( @str_replace( '\\' , '_' , __NAMESPACE__ ) ) . '_';
163  return ptc_run( 'fire' , @constant( $ptc_event ) , array( $event , $data ) );
164  }
165 
166  ### PHPTOOLCASE RUNNER UTILITY #############################
167 
168  /**
169  * Runs class methods
170  * @param string $function the function/method to run
171  * @param string $class the class name with it's full namespace
172  * @param array $args an array with arguments for the method
173  * @return the result of the call, or false if the method was not callable
174  */
175  function ptc_run( $function , $class = null , $args = array( ) )
176  {
177  $call = ( $class ) ? array( '\\' . $class , $function ) : $function;
178  if ( @is_callable( $call ) ) { return call_user_func_array( $call , $args ); }
179  //trigger_error( 'Could not run method "' . $function .'" for class "' . $class . '"' , E_USER_ERROR );
180  return false;
181  }
ptc_path($type=null)
Retrieves the application paths stored in the PtcHandyMan::$_appPaths array.
Definition: ptc-helpers.php:90
ptc_stop_timer($reference=null, $precision=1)
Monitors the execution of php code, or sql queries based on a reference.
Definition: ptc-helpers.php:43
ptc_add_dir($directories)
Adds directories to the autoloader to load classes.
ptc_add_file($files)
Adds files to the class autoloader.
ptc_run($function, $class=null, $args=array())
Runs class methods.
ptc_log_sql($string, $statement=null, $category=null)
Writes data to the sql panel.
Definition: ptc-helpers.php:34
ptc_listen($event, $callback, $priority=0)
Adds a listener to an event.
ptc_start_trace()
Starts the function calls trace utility.
Definition: ptc-helpers.php:77
ptc_attach($reference, $string, $statement=null)
Attaches data to the buffer array based on a reference.
Definition: ptc-helpers.php:53
ptc_stop_coverage()
Stop the code coverage analysis utility.
Definition: ptc-helpers.php:73
ptc_dir($type=null)
Retrieves the directories the autoloader uses to load classes.
ptc_fire($event, $data)
Fires an event See PtcEvent::fire( )
ptc_start_coverage()
Starts the code coverage analysis utility to find executed lines.
Definition: ptc-helpers.php:69
ptc_get_prop($class, $propertyName)
Gets protected and private properties.
ptc_stop_trace()
Stop the function calls trace utility.
Definition: ptc-helpers.php:81
ptc_log($string, $statement=null, $category=null)
Writes data to the messages panel.
Definition: ptc-helpers.php:24
ptc_add_path($paths)
Adds application paths to the PtcHandyMan::$_appPaths array.
Definition: ptc-helpers.php:99
ptc_watch($variableName, $callback=null)
Watches a variable that is in a declare(ticks=n){ code block }, for changes.
Definition: ptc-helpers.php:62