11<?php
2+ declare (strict_types=1 );
23
34namespace Cydrickn \PHPWatcher ;
45
6+ use Cydrickn \PHPWatcher \Adapters \AdapterInterface ;
57use Cydrickn \PHPWatcher \Enum \ChangeType ;
68use RecursiveDirectoryIterator ;
79use RecursiveTreeIterator ;
@@ -13,12 +15,11 @@ class Watcher
1315 private array $ changes = [];
1416 private bool $ initialized = false ;
1517 private bool $ checking = false ;
16-
1718 private array $ excludeFiles = [];
1819 private array $ excludeDirs = [];
1920 private mixed $ handler ;
2021
21- public function __construct (private array $ watchFor , array $ excludes , callable $ handler , private int $ interval = 1000 )
22+ public function __construct (private readonly AdapterInterface $ adapter , private readonly array $ watchFor , array $ excludes , callable $ handler , private readonly int $ interval = 1000 )
2223 {
2324 foreach ($ excludes as $ exclude ) {
2425 if (is_dir ($ exclude )) {
@@ -30,7 +31,6 @@ public function __construct(private array $watchFor, array $excludes, callable $
3031 $ this ->handler = $ handler ;
3132 }
3233
33-
3434 protected function addChange (string $ filename , ChangeType $ type ): void
3535 {
3636 $ this ->changes [] = [
@@ -63,7 +63,7 @@ protected function checkFile(string $file, bool $checkForDelete = false): void
6363 $ data = $ this ->files [$ file ];
6464 $ checkData = filemtime ($ file );
6565
66- if ($ checkData != $ data ) {
66+ if ($ checkData !== $ data ) {
6767 $ this ->addChange ($ file , ChangeType::UPDATED );
6868 }
6969 }
@@ -81,7 +81,6 @@ protected function commit(): void
8181 unset($ this ->files [$ change ['name ' ]]);
8282 continue ;
8383 }
84-
8584 $ this ->files [$ change ['name ' ]] = $ change ['data ' ];
8685 }
8786
@@ -106,11 +105,9 @@ public function checkChanges(): void
106105 foreach ($ allFiles as $ file ) {
107106 $ filename = trim (str_replace (['| ' , ' ' , '~ ' , '\\' ], '' , $ file ), '- ' );
108107 $ filename = is_dir ($ filename ) ? $ filename . '/ ' : $ filename ;
109-
110108 if ((is_file ($ filename ) && in_array ($ filename , $ this ->excludeFiles )) || (is_dir ($ filename ) && in_array ($ filename , $ this ->excludeDirs ))) {
111109 continue ;
112110 }
113-
114111 foreach ($ this ->excludeDirs as $ dir ) {
115112 if (str_starts_with ($ filename , $ dir )) {
116113 continue 2 ;
@@ -123,7 +120,6 @@ public function checkChanges(): void
123120 $ this ->checkFile ($ watchFor );
124121 }
125122
126-
127123 // Checks the deleted files
128124 foreach ($ this ->files as $ key => $ file ) {
129125 $ this ->checkFile ($ key , true );
@@ -138,24 +134,8 @@ public function checkChanges(): void
138134 $ this ->checking = false ;
139135 }
140136
141- public function tick (? callable $ handler = null ): void
137+ public function tick (): void
142138 {
143- if ($ handler !== null ) {
144- call_user_func ($ handler , [$ this , 'checkChanges ' ], $ this ->interval );
145- return ;
146- }
147-
148- if (class_exists (Timer::class)) {
149- Timer::tick ($ this ->interval , function () {
150- $ this ->checkChanges ();
151- });
152- } else {
153- $ this ->tick (function ($ checkChanges , $ interval ) {
154- do {
155- call_user_func ($ checkChanges );
156- sleep ($ interval / 1000 );
157- } while (true );
158- });
159- }
139+ $ this ->adapter ->tick ([$ this , 'checkChanges ' ], $ this ->interval );
160140 }
161141}
0 commit comments