1717- composer
1818
1919## Installation via Composer
20- To install, navigate to your project root directory (where ` composer.json ` is located) and run the following command:
21- ``` shell
22- composer require naingaunglwin-dev/timetracker
20+ > If Composer is not installed, follow the [ official guide] ( https://getcomposer.org/download/ ) .
21+
22+ 1 . Create a ` composer.json ` file at your project root directory (if you don't have one):
23+ ``` json
24+ {
25+ "require" : {
26+ "naingaunglwin-dev/timetracker" : " ^1.0"
27+ }
28+ }
29+ ```
30+
31+ - Run the following command in your terminal from the project's root directory:
32+ ``` bash
33+ composer install
2334```
24- - If ` composer.json ` doesn't exits, run this command first,
25- ``` shell
26- composer init
35+
36+ If you already have ` composer.json ` file in your project, just run this command in your terminal,
37+ ``` bash
38+ composer require naingaunglwin-dev/timetracker
2739```
2840
2941## Usage
@@ -40,7 +52,7 @@ $tracker->start('test');
4052echo 'hello world<br >';
4153sleep(3);
4254
43- $tracker->end ('test');
55+ $tracker->stop ('test');
4456
4557echo $tracker->calculate('test')
4658 ->get();
@@ -53,18 +65,12 @@ echo $tracker->calculate('test')
5365### Convert to different unit
5466- By default, the unit is in seconds (s). You can convert to other predefined units like milliseconds (ms), microseconds (us), and more:
5567``` php
56- <?php
57-
58- require 'vendor/autoload.php';
59-
60- $tracker = new NAL\TimeTracker\TimeTracker();
61-
6268$tracker->start('test');
6369
6470echo 'hello world<br >';
6571sleep(3);
6672
67- $tracker->end ('test');
73+ $tracker->stop ('test');
6874
6975echo $tracker->calculate('test')
7076 ->convert('ms')
@@ -78,18 +84,12 @@ echo $tracker->calculate('test')
7884### Add custom unit
7985- You can define custom units based on seconds (for example, converting seconds to custom units):
8086``` php
81- <?php
82-
83- require 'vendor/autoload.php';
84-
85- $tracker = new NAL\TimeTracker\TimeTracker();
86-
8787$tracker->start('test');
8888
8989echo 'hello world<br >';
9090sleep(3);
9191
92- $tracker->end ('test');
92+ $tracker->stop ('test');
9393
9494// Add a custom unit definition (1 second = 10 custom units)
9595$tracker->addUnitDefinition('testunit', '*', 10);
@@ -106,18 +106,12 @@ echo $tracker->calculate('test')
106106### Format output
107107- You can format the output of the calculated time using placeholders:
108108``` php
109- <?php
110-
111- require 'vendor/autoload.php';
112-
113- $tracker = new NAL\TimeTracker\TimeTracker();
114-
115109$tracker->start('test');
116110
117111echo 'hello world<br >';
118112sleep(3);
119113
120- $tracker->end ('test');
114+ $tracker->stop ('test');
121115
122116echo $tracker->calculate('test')
123117 ->convert('ms')
@@ -132,23 +126,21 @@ echo $tracker->calculate('test')
132126### Time tracking with callback function
133127- You can track time for a callback function and get both the execution time and the result:
134128``` php
135- $result = \NAL\TimeTracker\TimeTracker::watch(
136- function (Conversation $conv, $time) {
137- sleep(3);
138- return $conv->greet($time) . '<br >do something at ' . $time;
139- },
140- ['time' => 'evening'], //parameters variableName => value
141- 'ms' // time unit, default is `s`
142- );
143-
144129class Conversation
145130{
146131 public function greet($time){
147132 return 'good ' . $time;
148133 }
149134}
150135
151- var_dump($result);
136+ $watch = \NAL\TimeTracker\TimeTracker::watch(
137+ function (Conversation $conv, $time) {
138+ sleep(3);
139+ return $conv->greet($time) . '<br >do something at ' . $time;
140+ },
141+ ['time' => 'evening'], //parameters variableName => value
142+ 'ms' // time unit, default is `s`
143+ );
152144```
153145- Example output:
154146``` php
@@ -160,3 +152,50 @@ array (size=4)
160152 'unit' => string 'ms' (length=2)
161153 'output' => string 'good evening, do something at evening' (length=37)
162154```
155+
156+ ### Checking timer states
157+
158+ The following methods help you check timer states and get currently active timers.
159+
160+ #### Check if a timer has started
161+ ``` php
162+ $tracker->start('download');
163+
164+ if ($tracker->isStarted('download')) {
165+ echo "Download timer is started.";
166+ }
167+
168+ // Output:
169+ // Download timer is started.
170+ ```
171+
172+ #### Check if a timer has stopped
173+ ``` php
174+ $tracker->start('process');
175+
176+ sleep(1);
177+
178+ $tracker->stop('process');
179+
180+ if ($tracker->isStopped('process')) {
181+ echo "Process timer is stopped.";
182+ }
183+
184+ // Output:
185+ // Process timer is stopped.
186+ ```
187+
188+ #### Get currently active timers
189+ ``` php
190+ $tracker->start('task1');
191+ $tracker->start('task2');
192+ $tracker->stop('task1');
193+
194+ print_r($tracker->getActiveTimers());
195+
196+ // Output:
197+ // Array
198+ // (
199+ // [0] => task2
200+ // )
201+ ```
0 commit comments