Skip to content

[2.2] [WIP] ResourceWatcher component refactored #3961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 72 commits into from

Conversation

everzet
Copy link
Contributor

@everzet everzet commented Apr 16, 2012

REBASED TO #4605

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes (for old tests) and partly (InotifyTracker is not yet finished)
Fixes the following tickets: -

This is refactoring of #391, which:

  • Uses EventDispatcher component for events routing
  • Uses inotify naming convention for event types (IN_CREATE, IN_MODIFY, IN_DELETE).

Short overview

This component is here for tracking filesystem changes. You're registering directory or file resources to track with specific track name (tracking_id) and start() watcher. Watcher checks for FS changes after specific timespan and if some change occur (file/directory create/update/delete) - watcher dispatches EventDispatcher event with resource_watcher.all, resource_watcher.tracking_id names and FilesystemEvent as argument.

Usage

Simplest possible way of FS tracking

Simplest tracking code would look like this:

<?php

$watcher = new Symfony\Component\ResourceWatcher\ResourceWatcher;

// track for any change inside `config` subfolder:
$watcher->trackByListener(__DIR__.'/config', function($event) {
    echo $event->getTypeString().': '.(string) $event->getResource();
});

// start listening
$watcher->start();

best suitable Tracker and EventDispatcher would be instantiated for you, implicitly

track() method

Extended usage looks like that:

<?php

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\ResourceWatcher;
use Symfony\Component\Config\Resource;

$dispatcher = new EventDispatcher;
$tracker = new ResourceWatcher\Tracker\RecursiveIteratorTracker;
$watcher = new ResourceWatcher\ResourceWatcher($tracker, $dispatcher);

// track for any change in `view` subfolder to `twig.templates` tracker:
$watcher->track('twig.templates', __DIR__.'/view');

// track for `*.xml` files creations in `config` subfolder to `routing` tracker:
$watcher->track(
    'routing',
    Resource\DirectoryResource(__DIR__.'/config', '/\.xml$/'),
    ResourceWatcher\Event\FilesystemEvent::IN_CREATE
);

// add `routing` track listener:
$dispatcher->addListener('resource_watcher.routing', function($event) {
    echo $event->getTypeString().': '.(string) $event->getResource();
});

// add all tracks listener (listens to every tracked event):
$dispatcher->addListener('resource_watcher.all', function($event) {
    echo $event->getTypeString().': '.(string) $event->getResource();
});

// start listening
$watcher->start();

addListener() method

If you don't want to deal with EventDispatcher, ResourceWatcher can hide this detail for you:

<?php

use Symfony\Component\ResourceWatcher;
use Symfony\Component\Config\Resource;

$tracker = new ResourceWatcher\Tracker\RecursiveIteratorTracker;
$watcher = new ResourceWatcher\ResourceWatcher($tracker);

// track for any change in `view` subfolder to `twig.templates` tracker:
$watcher->track('twig.templates', __DIR__.'/view');

// track for `*.xml` files creations in `config` subfolder to `routing` tracker:
$watcher->track(
    'routing',
    Resource\DirectoryResource(__DIR__.'/config', '/\.xml$/'),
    ResourceWatcher\Event\FilesystemEvent::IN_CREATE
);

// add `routing` track listener:
$watcher->addListener('routing', function($event) {
    echo $event->getTypeString().': '.(string) $event->getResource();
});

// add all tracks listener (listens to every tracked event):
$watcher->addListener('all', function($event) {
    echo $event->getTypeString().': '.(string) $event->getResource();
});

// start listening
$watcher->start();

WIP

InotifyTracker is not ready as i have problems testing it (no Inotify on MacOS). Help here would be highly appreciated - test suite is ready, requirement is to make InotifyTrackerTest green.

everzet and others added 30 commits April 16, 2012 14:43
this is a basic state checker, which checks all
files and directories recursively for changes
this resources tracker uses recursive iterator state
checker internally
if no tracker were specified in watcher constructor
it will use best one (inotify if available and rec
otherwise)
Dattaya and others added 4 commits May 27, 2012 10:52
[ResourceWatcher] Few small issues
Makes sure that directory and the file resources
with the same name will have different ids
file resource existence check shouldn't return true if there's
directory with same name instead of file.
@everzet
Copy link
Contributor Author

everzet commented Jun 9, 2012

All RecursiveIteratorTracker-related tests pass and i'm quite happy with component. @Dattaya could you check that InotifyTracker still passes to?

@fabpot it's almost ready. We just need to have additional pair of eyes to check everything and that's it :)
@stof could you review?

@travisbot
Copy link

This pull request passes (merged 3aaf29b into e7470ff).

@travisbot
Copy link

This pull request passes (merged 2cf953d into e7470ff).

@stof
Copy link
Member

stof commented Jun 9, 2012

@everzet this PR conflicts with master according to github

@@ -63,3 +63,12 @@

system(sprintf('cd %s && git fetch origin && git reset --hard %s', escapeshellarg($installDir), escapeshellarg($rev)));
}

// install inotify extension
system(<<<SH
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should wrap this call as it will fail for Windows users, making the vendors script unusable.

Btw, is it really necessary ? Travis uses Composer now in master

@travisbot
Copy link

This pull request passes (merged 0564b07 into e7470ff).

@stof
Copy link
Member

stof commented Jun 11, 2012

@fabpot what about merging the changes done in the ResourceInterface into 2.1 to allow marking the Config component as stable instead of waiting for 2.2 to do it because of this PR ?
Btw, making the ResourceInterface extend Serializable indeed make sense as the resources are always serialized in the meta files.

@Dattaya
Copy link
Contributor

Dattaya commented Jun 13, 2012

@Dattaya could you check that InotifyTracker still passes to?

yep, it passes

All RecursiveIteratorTracker-related tests pass and i'm quite happy with component.

I think the RecursiveIteratorTracker is great. I'm out of ideas how to break it.

@everzet
Copy link
Contributor Author

everzet commented Jun 13, 2012

@stof @fabpot i could cherry-pick Config component changes from this PR.

@stof
Copy link
Member

stof commented Jun 13, 2012

@everzet this is what I had in mind. Let's see what @fabpot think about it.

@everzet
Copy link
Contributor Author

everzet commented Jun 13, 2012

Also as there's still no 1st beta for Symfony 2.1 AFAIK, we could think about including this component (in form of unstabilized one) to 2.1, cuz test suite passes for both trackers (recursive, inotify).

@fabpot @stof what do you think, guys?

@everzet
Copy link
Contributor Author

everzet commented Jun 13, 2012

@stof
Copy link
Member

stof commented Jun 13, 2012

@everzet why not forcing the push in the branch to update the PR ?

@everzet
Copy link
Contributor Author

everzet commented Jun 13, 2012

@stof to not break history of this PR. i'm thinking about opening another PR instead.

@everzet
Copy link
Contributor Author

everzet commented Jun 13, 2012

@fabpot can we really get rid of this IN_ prefix (that have been added for inotify event names compliance) or should i revert these 2 @Dattaya commits?

],
"require": {
"php": ">=5.3.3"
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the component requires symfony/config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @stofbot is right. This component does depends on symfony/config...

@fabpot
Copy link
Member

fabpot commented Jun 13, 2012

@everzet feel free to remove the IN_ prefix.

@everzet
Copy link
Contributor Author

everzet commented Jun 18, 2012

rebased to #4605

@everzet everzet closed this Jun 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants