Checks a list of user-defined directories for new file changes on every request. Useful for reactive programming during development to trigger automated tasks, like builds.
The API is very small!
use SilverStripe\ProjectWatcher\ProjectWatcher;
if (ProjectWatcher::singleton()->hasModifications()) {
// do something
}
$watcher = ProjectWatcher::singleton();
$time = $watcher->getTimestamp();
if ($watcher->hasModifications()) {
echo "You have modified files" . "<br>";
foreach ($watcher->getModifications() as $file) {
echo $file->getFilename() . "<br>";
}
} elseif ($time) {
echo "No modifications since $time";
}
ProjectWatcher::getModifications()
returns SplFileInfo
instances.
By default, the ProjectWatcher
service is configured to watch your project (app/
) directory,
and any composer dependencies that are Silverstripe modules checked out from repositories.
These are defined in the ProjectWatcher
constructor as DirectoryWatcher
and ComposerDependencyWatcher
services.
SilverStripe\Core\Injector\Injector:
SilverStripe\ProjectWatcher\ProjectWatcher:
properties:
watchers:
project: '%$SilverStripe\ProjectWatcher\DirectoryWatcher'
dependencies: '%$SilverStripe\ProjectWatcher\ComposerDependencyWatcher'
If you don't want to watch depdendencies, it is recommended that you disable the
ComposerDependencyWatcher
instance to increase performance.
SilverStripe\Core\Injector\Injector:
SilverStripe\ProjectWatcher\ProjectWatcher:
properties:
watchers:
dependencies: false
The DirectoryWatcher
that is added to the ProjectWatcher
instance by default comes
with presets that are common for most projects, but you may want to change these.
SilverStripe\Core\Injector\Injector:
SilverStripe\ProjectWatcher\DirectoryWatcher:
properties:
# Scan the entire app/ tree
paths: [ app ]
# Only look for changes to .php and .yml files
match: ['*.php', '*.yml']
You can customise this by adding more directories and matching patterns.
For more specificity, you can also set matching patterns per path:
SilverStripe\Core\Injector\Injector:
SilverStripe\ProjectWatcher\DirectoryWatcher:
properties:
paths:
# Watch all the .php and .yml files in app/
- [ 'app', ['*.php', '*.yml'] ]
# Watch for changes to .graphql files only in myFolder/
- [ 'myFolder', ['*.graphql'] ]