3333namespace ElementaryFramework \FireFS ;
3434
3535use ElementaryFramework \FireFS \Exceptions \FileSystemEntityNotFoundException ;
36+ use ElementaryFramework \FireFS \Listener \IFileSystemListener ;
37+ use ElementaryFramework \FireFS \Events \FileSystemEntityCreatedEvent ;
38+ use ElementaryFramework \FireFS \Events \FileSystemEntityDeletedEvent ;
39+ use ElementaryFramework \FireFS \Events \FileSystemEntityModifiedEvent ;
3640
3741/**
3842 * FireFS - Filesystem Manager Class
@@ -102,6 +106,14 @@ class FireFS
102106 */
103107 protected $ aliases = array ();
104108
109+ /**
110+ * The file system event listener of
111+ * this instance.
112+ *
113+ * @var IFileSystemListener
114+ */
115+ protected $ listener ;
116+
105117 /**
106118 * Class constructor
107119 *
@@ -146,6 +158,26 @@ public function setTempDir(string $path)
146158 $ this ->tempDir = $ this ->cleanPath ($ path );
147159 }
148160
161+ /**
162+ * Sets the file system listener of this instance.
163+ *
164+ * It's important to note that this listener will
165+ * used only to listen actions from this instance
166+ * of FireFS. It cannot listen for changes from the
167+ * outside.
168+ *
169+ * If you want to watch the file system for changes,
170+ * use the FileSystemWatcher class.
171+ *
172+ * @param IFileSystemListener $listener The event listener.
173+ *
174+ * @return void
175+ */
176+ public function setListener (IFileSystemListener $ listener )
177+ {
178+ $ this ->listener = $ listener ;
179+ }
180+
149181 /**
150182 * Clean the path for bad directory name
151183 *
@@ -399,6 +431,7 @@ public function write(string $path, string $data, bool $append = false): bool
399431 $ file = fopen ($ internalPath , "a " );
400432 if (fwrite ($ file , $ data ) !== false ) {
401433 fclose ($ file );
434+ $ this ->_onModify ($ path );
402435 return true ;
403436 } else {
404437 fclose ($ file );
@@ -415,6 +448,7 @@ public function write(string $path, string $data, bool $append = false): bool
415448 }
416449
417450 if (file_put_contents ($ internalPath , $ data ) !== false ) {
451+ $ this ->_onModify ($ path );
418452 if ($ applyChmod ) {
419453 chmod ($ internalPath , 0777 );
420454 }
@@ -465,6 +499,7 @@ public function mkfile(string $path, bool $createParent = false): bool
465499 throw new \RuntimeException ("Cannot create file \"{$ path }\"" );
466500 } else {
467501 chmod ($ internalPath , 0777 );
502+ $ this ->_onCreate ($ path );
468503 return true ;
469504 }
470505 }
@@ -581,6 +616,7 @@ public function mkdir(string $path, bool $recursive = false): bool
581616 throw new \RuntimeException ("Cannot create directory \"{$ path }\"" );
582617 } else {
583618 chmod ($ internalPath , 0777 );
619+ $ this ->_onCreate ($ path );
584620 return true ;
585621 }
586622 }
@@ -628,6 +664,8 @@ public function move(string $path, string $new_path): bool
628664 if (!rename ($ sourceInternalPath , $ destInternalPath )) {
629665 throw new \RuntimeException ("Cannot move file from \"{$ path }\" to \"{$ new_path }\"" );
630666 } else {
667+ $ this ->_onDelete ($ path );
668+ $ this ->_onCreate ($ new_path );
631669 return true ;
632670 }
633671 }
@@ -690,6 +728,7 @@ public function copy(string $path, string $new_path): bool
690728 if (copy ($ sourceInternalPath , $ destInternalPath ) === false ) {
691729 throw new \RuntimeException ("Cannot copy file from \"{$ path }\" to \"{$ new_path }\"" );
692730 } else {
731+ $ this ->_onCreate ($ new_path );
693732 return true ;
694733 }
695734 }
@@ -943,12 +982,14 @@ public function delete(string $path, bool $recursive = false): bool
943982 if (rmdir ($ internalPath ) === false ) {
944983 throw new \RuntimeException ("Cannot delete directory \"{$ path }\". " );
945984 } else {
985+ $ this ->_onDelete ($ path );
946986 return true ;
947987 }
948988 } else {
949989 if (unlink ($ internalPath ) === false ) {
950990 throw new \RuntimeException ("Cannot delete file \"{$ path }\". " );
951991 } else {
992+ $ this ->_onDelete ($ path );
952993 return true ;
953994 }
954995 }
@@ -1124,4 +1165,40 @@ public function getEntity(string $path)
11241165 throw new FileSystemEntityNotFoundException ($ path );
11251166 }
11261167 }
1168+
1169+ /**
1170+ * Raise a "create" event.
1171+ *
1172+ * @param string $path The path of the entity which raised the event.
1173+ */
1174+ private function _onCreate (string $ path )
1175+ {
1176+ if ($ this ->listener instanceof IFileSystemListener) {
1177+ $ this ->listener ->onCreated (new FileSystemEntityCreatedEvent ($ path ));
1178+ }
1179+ }
1180+
1181+ /**
1182+ * Raise a "modify" event.
1183+ *
1184+ * @param string $path The path of the entity which raised the event.
1185+ */
1186+ private function _onModify (string $ path )
1187+ {
1188+ if ($ this ->listener instanceof IFileSystemListener) {
1189+ $ this ->listener ->onCreated (new FileSystemEntityModifiedEvent ($ path ));
1190+ }
1191+ }
1192+
1193+ /**
1194+ * Raise a "delete" event.
1195+ *
1196+ * @param string $path The path of the entity which raised the event.
1197+ */
1198+ private function _onDelete (string $ path )
1199+ {
1200+ if ($ this ->listener instanceof IFileSystemListener) {
1201+ $ this ->listener ->onCreated (new FileSystemEntityDeletedEvent ($ path ));
1202+ }
1203+ }
11271204}
0 commit comments