Skip to content

silverstripe/silverstripe-project-watcher

Repository files navigation

Silverstripe Project Watcher Module

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.

Usage

The API is very small!

Check if there are changes to your project since the last request

use SilverStripe\ProjectWatcher\ProjectWatcher;

if (ProjectWatcher::singleton()->hasModifications()) {
  // do something
}

Get the changed files

$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.

Configuring

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

Scanning the right files in your project

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'] ]

About

A module that watches your project for file changes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages