Skip to content

Commit cc9aac1

Browse files
committed
working version of the plugin
1 parent fb935fa commit cc9aac1

File tree

6 files changed

+70
-69
lines changed

6 files changed

+70
-69
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Reload Plugin for Pattern Lab
55

6-
The Reload Plugin adds Web Socket-based automatic browser reload support to Pattern Lab. The Reload Plugin will automatically reload your browser window if you're using the `--watch` flag.
6+
The Reload Plugin adds Web Socket-based automatic browser reload support to Pattern Lab. The Reload Plugin will automatically reload the Pattern Lab iFrame if you're using the `--watch` flag.
77

88
## Installation
99

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"require": {
2727
"php": ">=5.4",
2828
"pattern-lab/core": "dev-dev",
29-
"wrench/wrench": "^2.0.0"
29+
"wrench/wrench": "^2.0.0",
30+
"cocur/background-process": "~0.5"
3031
},
3132
"extra": {
3233
"patternlab": {

dist/js/plugin-reload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var PluginReload = {
22

33
init: function() {
4-
4+
55
if ('WebSocket' in window && window.WebSocket.CLOSING === 2) {
66

7-
wsc = new WebSocket("ws://"+host+":8001/autoreload");
7+
wsc = new WebSocket("ws://localhost:8000/reload");
88

99
// when trying to open a connection to WebSocket update the pattern lab nav bar
1010
wsc.onopen = function () {

src/PatternLab/Reload/AutoReloadApplication.php

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,60 +18,61 @@
1818
use \Wrench\Application\NamedApplication;
1919

2020
class AutoReloadApplication extends Application {
21-
22-
protected $clients = array();
23-
protected $savedTimestamp = time();
24-
protected $savedTimestampPath = __DIR__."/../../../../../../public/latest-change.txt";
25-
26-
/**
27-
* Set the saved timestamp. If the latest-change file doesn't exist simply use the current time as the saved time
28-
*/
29-
public function __construct() {
30-
31-
if (file_exists($savedTimestampPath)) {
32-
$this->savedTimestamp = file_get_contents($savedTimestampPath);
33-
}
34-
35-
}
36-
37-
/**
38-
* When a client connects add it to the list of connected clients
39-
*/
40-
public function onConnect($client) {
41-
$id = $client->getId();
42-
$this->clients[$id] = $client;
43-
}
44-
45-
/**
46-
* When a client disconnects remove it from the list of connected clients
47-
*/
48-
public function onDisconnect($client) {
49-
$id = $client->getId();
50-
unset($this->clients[$id]);
51-
}
52-
53-
/**
54-
* Dead function in this instance
55-
*/
56-
public function onData($data, $client) {
57-
// function not in use
58-
}
59-
60-
/**
61-
* Sends out a message once a second to all connected clients containing the contents of latest-change.txt
62-
*/
63-
public function onUpdate() {
64-
65-
if (file_exists($savedTimestampPath)) {
66-
$readTimestamp = file_get_contents($savedTimestampPath);
67-
if ($readTimestamp != $this->savedTimestamp) {
68-
foreach ($this->clients as $sendto) {
69-
$sendto->send($readTimestamp);
70-
}
71-
$this->savedTimestamp = $readTimestamp;
72-
}
73-
}
74-
75-
}
76-
21+
22+
protected $clients = array();
23+
protected $savedTimestamp = '';
24+
protected $savedTimestampPath = '';
25+
26+
/**
27+
* Set the saved timestamp. If the latest-change file doesn't exist simply use the current time as the saved time
28+
*/
29+
public function __construct() {
30+
$this->savedTimestampPath = __DIR__."/../../../../../../public/latest-change.txt";
31+
if (file_exists($this->savedTimestampPath)) {
32+
$this->savedTimestamp = file_get_contents($this->savedTimestampPath);
33+
} else {
34+
$this->savedTimestamp = time();
35+
}
36+
}
37+
38+
/**
39+
* When a client connects add it to the list of connected clients
40+
*/
41+
public function onConnect($client) {
42+
$id = $client->getId();
43+
$this->clients[$id] = $client;
44+
}
45+
46+
/**
47+
* When a client disconnects remove it from the list of connected clients
48+
*/
49+
public function onDisconnect($client) {
50+
$id = $client->getId();
51+
unset($this->clients[$id]);
52+
}
53+
54+
/**
55+
* Dead function in this instance
56+
*/
57+
public function onData($data, $client) {
58+
// function not in use
59+
}
60+
61+
/**
62+
* Sends out a message once a second to all connected clients containing the contents of latest-change.txt
63+
*/
64+
public function onUpdate() {
65+
66+
if (file_exists($this->savedTimestampPath)) {
67+
$readTimestamp = file_get_contents($this->savedTimestampPath);
68+
if ($readTimestamp != $this->savedTimestamp) {
69+
foreach ($this->clients as $sendto) {
70+
$sendto->send($readTimestamp);
71+
}
72+
$this->savedTimestamp = $readTimestamp;
73+
}
74+
}
75+
76+
}
77+
7778
}

src/PatternLab/Reload/AutoReloadServer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* Copyright (c) 2013-2016 Dave Olsen, http://dmolsen.com
77
* Licensed under the MIT license
88
*
9-
* The server that clients attach to to learn about content updates. See
10-
* lib/Wrench/Application/contentSyncBroadcasterApplication.php for logic
9+
* The web socket server that clients attach to to learn about content updates.
1110
*
1211
*/
1312

14-
require("../../../../../autoload.php");
13+
require(__DIR__."/../../../../../autoload.php");
1514

16-
$server = new \Wrench\BasicServer('ws://0.0.0.0:8001');
15+
$server = new \Wrench\BasicServer('ws://localhost:8000', array('allowed_origins' => array('localhost')));
1716
$server->registerApplication('reload', new \PatternLab\Reload\AutoReloadApplication());
1817
$server->run();

src/PatternLab/Reload/PatternLabListener.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212

1313
namespace PatternLab\Reload;
1414

15+
use \Cocur\BackgroundProcess\BackgroundProcess;
1516
use \PatternLab\Config;
1617
use \PatternLab\Console;
1718
use \PatternLab\Data;
1819

1920
class PatternLabListener extends \PatternLab\Listener {
2021

21-
protected $faker;
22-
protected $locale;
23-
2422
/**
2523
* Add the listeners for this plug-in
2624
*/
@@ -37,8 +35,10 @@ public function __construct() {
3735
public function initServer() {
3836

3937
if ((bool)Config::getOption("reload.on")) {
40-
$path = __DIR__."AutoReloadServer.php";
41-
$fp = popen("php ".$path, "r");
38+
$php = isset($_SERVER["_"]) ? $_SERVER["_"] : Config::getOption("phpBin");
39+
$path = __DIR__."/AutoReloadServer.php";
40+
$process = new BackgroundProcess($php." ".$path);
41+
$process->run();
4242
}
4343

4444
}

0 commit comments

Comments
 (0)