Completed
Push — master ( a7122d...c2f186 )
by Tim
31s queued 11s
created

Watch   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 93
rs 10
c 0
b 0
f 0
ccs 0
cts 31
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A monitor() 0 14 3
A events() 0 5 1
B run() 0 32 4
1
<?php
2
3
/**
4
 * AppserverIo\RoboTasks\Base\Watch
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/robo-tasks
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RoboTasks\Base;
22
23
use Robo\Task\BaseTask;
24
use Lurker\ResourceWatcher;
25
use Lurker\Event\FilesystemEvent;
26
27
/**
28
 * Alternative watch task. The difference to the default watch task is, that this task
29
 * will by default watch also the delete + create events.
30
 *
31
 * If only specific events should be watched, use the `events()` method to define them.
32
 *
33
 * ```php
34
 * <?php
35
 * $this->taskWatch()
36
 *  ->monitor('composer.json', function() {
37
 *      $this->taskComposerUpdate()->run();
38
 * })->monitor('src', function() {
39
 *      $this->taskExec('phpunit')->run();
40
 * })->run();
41
 * ?>
42
 * ```
43
 *
44
 * @author    Tim Wagner <[email protected]>
45
 * @copyright 2015 TechDivision GmbH <[email protected]>
46
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
47
 * @link      https://github.com/appserver-io/robo-tasks
48
 * @link      http://www.appserver.io
49
 */
50
class Watch extends BaseTask
51
{
52
53
    /**
54
     * The array with the paths that has to be monitored.
55
     *
56
     * @var array
57
     */
58
    protected $monitor = array();
59
60
    /**
61
     * The event mask to use, by default we watch ALL events.
62
     *
63
     * @var integer
64
     */
65
    protected $eventMask = FilesystemEvent::ALL;
66
67
    /**
68
     * Set the paths to watch and the callback to execute.
69
     *
70
     * @param string|string[] $paths    The paths to watch
71
     * @param callable        $callable The callback that has to be executed on a change
72
     *
73
     * @return \AppserverIo\RoboTasks\Base\Watch The task instance
74
     * @throws \Exception Is thrown, if the passed paths is nor a string or an array
75
     */
76
    public function monitor($paths, callable $callable)
77
    {
78
79
        // convert the paths to an array if necessary
80
        if (is_string($paths)) {
81
            $paths = array($paths);
82
        } elseif (!is_array($paths)) {
83
            throw new \Exception(sprintf('Passed paths has either to be a string or an array'));
84
        }
85
86
        // append the paths
87
        $this->monitor[] = array($paths, $callable);
88
        return $this;
89
    }
90
91
    /**
92
     * Set the events to watch.
93
     *
94
     * @param integer $eventMask The event mask
95
     *
96
     * @return \AppserverIo\RoboTasks\Base\Watch The task instance
97
     */
98
    public function events($eventMask)
99
    {
100
        $this->eventMask = $eventMask;
101
        return $this;
102
    }
103
104
    /**
105
     * Invokes the task and its functionality.
106
     *
107
     * @return \Robo\Result The tasks result
108
     * @see \Robo\Contract\TaskInterface::run()
109
     */
110
    public function run()
111
    {
112
113
        // query whether or not the lurker library has been installed
114
        if (!class_exists('Lurker\\ResourceWatcher')) {
115
            return Result::errorMissingPackage($this, 'ResourceWatcher', 'henrikbjorn/lurker');
116
        }
117
118
        // intialize the watcher
119
        $watcher = new ResourceWatcher();
120
121
        // iterate over the directories that has to be watched
122
        foreach ($this->monitor as $k => $monitor) {
123
            // iterate over the directories that has to be watched
124
            foreach ($monitor[0] as $i => $dir) {
125
                // watch dir given directory
126
                $watcher->track("fs.$k.$i", $dir, $this->eventMask);
127
128
                // write a log message
129
                $this->printTaskInfo('Watching {dir} for changes...', ['dir' => $dir]);
130
131
                // add the listerner for the directory
132
                $watcher->addListener("fs.$k.$i", $monitor[1]);
133
            }
134
        }
135
136
        // start the watcher itself
137
        $watcher->start();
138
139
        // return the result
140
        return Result::success($this);
141
    }
142
}
143