Skip to content

Commit 137cae7

Browse files
authored
Merge pull request #61 from SimonFrings/loop
Simplify usage by supporting new default loop
2 parents a773b60 + 374ac84 commit 137cae7

13 files changed

+54
-84
lines changed

README.md

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ with PHP.
2525
* [waitFor()](#waitfor)
2626
* [launch()](#launch)
2727
* [launchZen()](#launchzen)
28-
* [Mixing synchronous and asynchronous PHP](#mixing-synchronous-and-asynchronous-php)
2928
* [Builder](#builder)
3029
* [Dialog](#dialog)
3130
* [AbstractDialog](#abstractdialog)
@@ -62,8 +61,7 @@ Once [installed](#install), you can use the following code to open a prompt
6261
asking the user for his name and presenting it in another info dialog.
6362

6463
```php
65-
$loop = Factory::create();
66-
$launcher = new Launcher($loop);
64+
$launcher = new Clue\React\Zenity\Launcher();
6765

6866
$entry = new EntryDialog();
6967
$entry->setText('What\'s your name?');
@@ -72,8 +70,6 @@ $entry->setEntryText(getenv('USER')); // prefill with current user
7270
$launcher->launch($entry)->then(function ($name) use ($launcher) {
7371
$launcher->launch(new InfoDialog('Welcome to zenity-react, ' . $name .'!'));
7472
});
75-
76-
$loop->run();
7773
```
7874

7975
Looking for more examples? Take a look at the [examples](examples) folder.
@@ -91,9 +87,14 @@ to enable an async workflow where you can launch multiple dialogs while simultan
9187
doing more I/O work. This library exposes both a simple blocking API and a more
9288
advanced async API.
9389

90+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
91+
pass the event loop instance to use for this object. You can use a `null` value
92+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
93+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
94+
given event loop instance.
95+
9496
```php
95-
$loop = React\EventLoop\Factory::create();
96-
$launcher = new Launcher($loop);
97+
$launcher = new Clue\React\Zenity\Launcher();
9798
```
9899

99100
#### setBin()
@@ -145,7 +146,7 @@ Some dialog types also support modifying the information presented to the user.
145146

146147
```php
147148
$zen = $launcher->launchZen($dialog);
148-
$loop->addTimer(3.0, function () use ($zen) {
149+
Loop::addTimer(3.0, function () use ($zen) {
149150
$zen->close();
150151
});
151152

@@ -154,27 +155,6 @@ $zen->promise()->then(function ($result) {
154155
});
155156
```
156157

157-
#### Mixing synchronous and asynchronous PHP ####
158-
ReactPHP expects all PHP to be non-blocking. Therefore it's not easily possible to use launch or launchZen followed by regular blocking events in PHP. Currently there is a simple but dirty workaround. It's possible to manually tick the loop to have changes on zen-objects take effect.
159-
160-
```php
161-
$progress = new ProgressDialog('Step 1');
162-
$progress->setPulsate(TRUE);
163-
$progress->setAutoClose(TRUE);
164-
$progress_zen = $launcher->launchZen($progress);
165-
166-
// This regular command is blocking and breaks the asynchronous workflow
167-
$hostname = gethostname();
168-
169-
$progress_zen->setText('Step 2');
170-
$loop->tick();
171-
172-
// SQL is also regular blocking PHP
173-
$get_sync = $db_serv->prepare('SELECT last_sync FROM tbl_sync WHERE hostname=?');
174-
$get_sync->execute(array($hostname));
175-
$result_sync = $get_sync->fetch();
176-
```
177-
178158
### Builder
179159

180160
Additionally, the `Builder` implements an even simpler interface for commonly
@@ -345,7 +325,7 @@ Zenity it not officially supported on other platforms, however several non-offic
345325
This library assumes Zenity is installed in your PATH. If it is not, you can explicitly set its path like this:
346326

347327
```php
348-
$launcher = new Launcher($loop);
328+
$launcher = new Clue\React\Zenity\Launcher();
349329
$launcher->setBin('/path/to/zenity');
350330
```
351331

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
],
1313
"require": {
1414
"php": ">=5.3",
15-
"react/child-process": "^0.6 || ^0.5 || ^0.4 || ^0.3",
16-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
15+
"react/child-process": "^0.6.3",
16+
"react/event-loop": "^1.2",
1717
"react/promise": "~2.0|~1.0"
1818
},
1919
"require-dev": {

examples/01-dialog.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Zenity\Launcher;
54
use Clue\React\Zenity\Dialog\InfoDialog;
65
use Clue\React\Zenity\Dialog\QuestionDialog;
@@ -10,9 +9,7 @@
109

1110
require __DIR__ . '/../vendor/autoload.php';
1211

13-
$loop = Factory::create();
14-
15-
$launcher = new Launcher($loop);
12+
$launcher = new Launcher();
1613

1714
$q = new EntryDialog('What\'s your name?');
1815
$q->setEntryText(getenv('USER'));
@@ -29,5 +26,3 @@
2926
}, function () use ($launcher) {
3027
$launcher->launch(new WarningDialog('No name given'));
3128
});
32-
33-
$loop->run();

examples/02-file.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Zenity\Launcher;
54
use Clue\React\Zenity\Dialog\FileSelectionDialog;
65
use Clue\React\Zenity\Builder;
76
use Clue\React\Zenity\Dialog\InfoDialog;
87

98
require __DIR__ . '/../vendor/autoload.php';
109

11-
$loop = Factory::create();
12-
13-
$launcher = new Launcher($loop);
10+
$launcher = new Launcher();
1411
$builder = new Builder();
1512

1613
$launcher->launch($builder->fileSelection())->then(function (SplFileInfo $file) use ($launcher) {
@@ -26,5 +23,3 @@
2623
$launcher->launch($selection);
2724
});
2825
});
29-
30-
$loop->run();

examples/03-progress-pulsate.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Zenity\Launcher;
54
use Clue\React\Zenity\Builder;
5+
use React\EventLoop\Loop;
66

77
require __DIR__ . '/../vendor/autoload.php';
88

9-
$loop = Factory::create();
10-
11-
$launcher = new Launcher($loop);
9+
$launcher = new Launcher();
1210
$builder = new Builder();
1311

1412
$progress = $launcher->launchZen($builder->pulsate('Pseudo-processing...'));
@@ -24,7 +22,7 @@
2422
'Finishing'
2523
);
2624

27-
$timer = $loop->addPeriodicTimer(2.0, function ($timer) use ($progress, $texts) {
25+
$timer = Loop::addPeriodicTimer(2.0, function ($timer) use ($progress, $texts) {
2826
static $i = 0;
2927

3028
if (isset($texts[$i])) {
@@ -48,5 +46,3 @@
4846

4947
$launcher->launch($builder->error('Canceled'));
5048
});
51-
52-
$loop->run();

examples/04-progress-random.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Zenity\Launcher;
54
use Clue\React\Zenity\Builder;
5+
use React\EventLoop\Loop;
66

77
require __DIR__ . '/../vendor/autoload.php';
88

9-
$loop = Factory::create();
10-
11-
$launcher = new Launcher($loop);
9+
$launcher = new Launcher();
1210
$builder = new Builder();
1311

1412
$progress = $launcher->launchZen($builder->progress('Pseudo-processing...'));
1513

1614
$progress->setPercentage(50);
1715

18-
$timer = $loop->addPeriodicTimer(0.2, function () use ($progress) {
16+
$timer = Loop::addPeriodicTimer(0.2, function () use ($progress) {
1917
$progress->advance(mt_rand(-1, 3));
2018
});
2119

@@ -29,5 +27,3 @@ function() use ($timer) {
2927
$timer->cancel();
3028
}
3129
);
32-
33-
$loop->run();

examples/05-form.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Zenity\Launcher;
54
use Clue\React\Zenity\Dialog\FormsDialog;
65

76
require __DIR__ . '/../vendor/autoload.php';
87

9-
$loop = Factory::create();
10-
$launcher = new Launcher($loop);
8+
$launcher = new Launcher();
119

1210
$form = new FormsDialog();
1311
$form->setWindowIcon('info');
@@ -25,5 +23,3 @@
2523
}, function() {
2624
var_dump('form canceled');
2725
});
28-
29-
$loop->run();

examples/06-menu.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Zenity\Launcher;
54
use Clue\React\Zenity\Builder;
65
use Clue\React\Zenity\Dialog\InfoDialog;
76

87
require __DIR__ . '/../vendor/autoload.php';
98

10-
$loop = Factory::create();
11-
12-
$launcher = new Launcher($loop);
9+
$launcher = new Launcher();
1310
$builder = new Builder();
1411

1512
$main = function() use (&$main, $builder, $launcher) {
@@ -53,5 +50,3 @@
5350
};
5451

5552
$main();
56-
57-
$loop->run();

examples/10-notification.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Zenity\Launcher;
54
use Clue\React\Zenity\Builder;
5+
use React\EventLoop\Loop;
66

77
require __DIR__ . '/../vendor/autoload.php';
88

9-
$loop = Factory::create();
10-
11-
$launcher = new Launcher($loop);
9+
$launcher = new Launcher();
1210
$builder = new Builder();
1311

1412
$notification = $builder->notifier();
@@ -17,11 +15,9 @@
1715
$zen->setMessage('Hello world');
1816

1917
$n = 0;
20-
$loop->addPeriodicTimer(10.0, function ($timer) use ($zen, &$n) {
18+
Loop::addPeriodicTimer(10.0, function ($timer) use ($zen, &$n) {
2119
static $icons = array('error', 'warning', 'info', '');
2220
$zen->setIcon($icons[array_rand($icons)]);
2321

2422
$zen->setMessage('Hi' . ++$n);
2523
});
26-
27-
$loop->run();

examples/20-blocking.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Zenity\Launcher;
54
use Clue\React\Zenity\Builder;
65
use Clue\React\Zenity\Dialog\EntryDialog;
76

87
require __DIR__ . '/../vendor/autoload.php';
98

10-
$loop = Factory::create();
11-
12-
$launcher = new Launcher($loop);
9+
$launcher = new Launcher();
1310
$builder = new Builder();
1411

1512
$name = $launcher->waitFor(new EntryDialog('Search package'));

src/Launcher.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Clue\React\Zenity;
44

5+
use React\EventLoop\Loop;
56
use React\EventLoop\LoopInterface;
67
use Clue\React\Zenity\Dialog\AbstractDialog;
78
use React\Promise\Deferred;
@@ -14,20 +15,26 @@
1415
*/
1516
class Launcher
1617
{
18+
/** @var LoopInterface */
1719
private $loop;
20+
1821
private $processLauncher;
1922
private $bin = 'zenity';
2023

21-
public function __construct(LoopInterface $loop, $processLauncher = null)
24+
/**
25+
* @param ?LoopInterface $loop
26+
* @param ?Callable $processLauncher
27+
*/
28+
public function __construct(LoopInterface $loop = null, $processLauncher = null)
2229
{
2330
if ($processLauncher === null) {
2431
$processLauncher = function ($cmd) {
2532
return new Process($cmd);
2633
};
2734
}
2835

36+
$this->loop = $loop ?: Loop::get();
2937
$this->processLauncher = $processLauncher;
30-
$this->loop = $loop;
3138
}
3239

3340
public function setBin($bin)

tests/LauncherTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Clue\Tests\React\Zenity;
4+
5+
use Clue\React\Zenity\Launcher;
6+
7+
class LauncherTest extends TestCase
8+
{
9+
public function testConstructWithoutLoopAssignsLoopAutomatically()
10+
{
11+
$launcher = new Launcher();
12+
13+
$ref = new \ReflectionProperty($launcher, 'loop');
14+
$ref->setAccessible(true);
15+
$loop = $ref->getValue($launcher);
16+
17+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
18+
}
19+
}

tests/Zen/BaseZenTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

33
use Clue\React\Zenity\Zen\BaseZen;
4-
use React\EventLoop\Factory;
5-
use React\ChildProcess\Process;
64
use Clue\Tests\React\Zenity\TestCase;
75

86
abstract class BaseZenTest extends TestCase

0 commit comments

Comments
 (0)