Skip to content

Commit 5ed35c0

Browse files
committed
Refactored for Leaf 1.0
1 parent 73979d7 commit 5ed35c0

12 files changed

+121
-74
lines changed

docs/index.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ The normal pattern for using a full focus background task is to host it as a 'st
188188
After the background task has started (triggered by the click of a button for example), the step is changed
189189
to the full focus presenter which has already been configured with the full focus presenter.
190190

191-
The BackgroundTaskFullFocusPresenter will trigger a server side event when the task completes and this will be
191+
The BackgroundTaskFullFocus will trigger a server side event when the task completes and this will be
192192
either as a normal post back, or via an XHR request. It can additionally be configured to redirect to a target URL
193193
(e.g. a payment complete page) instead of handling the event directly.
194194

195-
A BackgroundTaskFullFocusPresenter must be constructed with a view that extends BackgroundTaskFullFocusView as an
195+
A BackgroundTaskFullFocus must be constructed with a view that extends BackgroundTaskFullFocusView as an
196196
argument to supply the content for the 'holding' page.
197197

198198
~~~ php
@@ -202,7 +202,7 @@ class MySwitchedPresenter extends SwitchedPresenter
202202
{
203203
$presenters = [
204204
"payment" => $payment = new PaymentDetailsPresenter(),
205-
"please-wait" => $pleaseWait = new BackgroundTaskFullFocusPresenter( new PaymentProcessingView() ),
205+
"please-wait" => $pleaseWait = new BackgroundTaskFullFocus( new PaymentProcessingView() ),
206206
"thanks" => new ThanksPresenter()
207207
];
208208

@@ -244,7 +244,7 @@ class MySwitchedPresenter extends SwitchedPresenter
244244
{
245245
$presenters = [
246246
"payment" => $payment = new PaymentDetailsPresenter(),
247-
"please-wait" => $pleaseWait = new BackgroundTaskFullFocusPresenter( new PaymentProcessingView() ),
247+
"please-wait" => $pleaseWait = new BackgroundTaskFullFocus( new PaymentProcessingView() ),
248248
"thanks" => new ThanksPresenter()
249249
];
250250

@@ -311,9 +311,9 @@ class DemoView extends HtmlView
311311
{
312312
public $longTaskId = null;
313313

314-
protected function createPresenters()
314+
protected function createSubLeaves()
315315
{
316-
$this->addPresenters(
316+
$this->registerSubLeaf(
317317
new Button( "DoLongTask", "Reticulate The Splines", function()
318318
{
319319
$this->raiseEvent( "StartLongTask" );
@@ -346,13 +346,13 @@ class DemoView extends HtmlView
346346

347347
Sometimes you want to show a progress bar on the site not as a direct consequence of the user interacting
348348
with your page, but as a consequence of **any** user starting a background task. This is quite simple to
349-
achieve - simply create a BackgroundTaskProgressPresenter and set it's task id like this:
349+
achieve - simply create a BackgroundTask and set it's task id like this:
350350

351351
~~~ php
352-
// In createPresenters():
352+
// In createSubLeaves():
353353

354-
$this->AddPresenters(
355-
$progress = new BackgroundTaskProgressPresenter( "Progress" )
354+
$this->registerSubLeaf(
355+
$progress = new BackgroundTask( "Progress" )
356356
);
357357

358358
try {

src/BackgroundTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function initiate($settings = [])
7979
$additionalArgumentString .= escapeshellarg($argument);
8080
}
8181

82-
$command = "/usr/bin/env php " . realpath("vendor/rhubarbphp/rhubarb/platform/execute-cli.php") . " " . realpath(__DIR__ . "/Scripts/task-runner.php") . " " . escapeshellarg(get_called_class()) . " " . $task->BackgroundTaskStatusID . " " . $additionalArgumentString . " > /dev/null 2>&1 &";
82+
$command = "/usr/bin/env php " . realpath(VENDOR_DIR."/rhubarbphp/rhubarb/platform/execute-cli.php") . " " . realpath(__DIR__ . "/Scripts/task-runner.php") . " " . escapeshellarg(get_called_class()) . " " . $task->BackgroundTaskStatusID . " " . $additionalArgumentString . " > /dev/null 2>&1 &";
8383

8484
Log::debug("Launching background task " . $task->UniqueIdentifier, "BACKGROUND", $command);
8585

src/Presenters/BackgroundTaskPresenter.php renamed to src/Leaves/BackgroundTask.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,31 @@
1616
* limitations under the License.
1717
*/
1818

19-
namespace Rhubarb\Scaffolds\BackgroundTasks\Presenters;
19+
namespace Rhubarb\Scaffolds\BackgroundTasks\Leaves;
2020

21-
use Rhubarb\Leaf\Presenters\HtmlPresenter;
21+
use Rhubarb\Leaf\Leaves\HtmlPresenter;
22+
use Rhubarb\Leaf\Leaves\Leaf;
23+
use Rhubarb\Leaf\Leaves\LeafModel;
2224
use Rhubarb\Scaffolds\BackgroundTasks\Models\BackgroundTaskStatus;
2325

24-
class BackgroundTaskPresenter extends HtmlPresenter
26+
abstract class BackgroundTask extends Leaf
2527
{
26-
public function setBackgroundTaskStatusId($backgroundTaskStatusId)
27-
{
28-
$this->model->BackgroundTaskStatusID = $backgroundTaskStatusId;
29-
}
28+
/**
29+
* @var BackgroundTaskModel
30+
*/
31+
protected $model;
3032

31-
protected function getPublicModelPropertyList()
33+
public function setBackgroundTaskStatusId($backgroundTaskStatusId)
3234
{
33-
$properties = parent::getPublicModelPropertyList();
34-
$properties[] = "BackgroundTaskStatusID";
35-
36-
return $properties;
35+
$this->model->backgroundTaskStatusId = $backgroundTaskStatusId;
3736
}
3837

39-
protected function configureView()
38+
protected function onModelCreated()
4039
{
41-
parent::configureView();
40+
parent::onModelCreated();
4241

43-
$this->view->attachEventHandler("GetProgress", function () {
44-
$status = new BackgroundTaskStatus($this->model->BackgroundTaskStatusID);
42+
$this->model->getProgressEvent->attachHandler(function(){
43+
$status = new BackgroundTaskStatus( $this->model->backgroundTaskStatusId );
4544

4645
$progress = new \stdClass();
4746
$progress->percentageComplete = $status->PercentageComplete;
@@ -52,4 +51,14 @@ protected function configureView()
5251
return $progress;
5352
});
5453
}
54+
55+
/**
56+
* Should return a class that derives from LeafModel
57+
*
58+
* @return LeafModel
59+
*/
60+
protected function createModel()
61+
{
62+
return new BackgroundTaskModel();
63+
}
5564
}

src/Presenters/BackgroundTaskFullFocusPresenter.php renamed to src/Leaves/BackgroundTaskFullFocus.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,46 @@
1616
* limitations under the License.
1717
*/
1818

19-
namespace Rhubarb\Scaffolds\BackgroundTasks\Presenters;
19+
namespace Rhubarb\Scaffolds\BackgroundTasks\Leaves;
2020

21-
require_once __DIR__ . '/BackgroundTaskPresenter.php';
22-
23-
use Rhubarb\Crown\Context;
21+
use Rhubarb\Crown\Application;
22+
use Rhubarb\Crown\Events\Event;
2423
use Rhubarb\Scaffolds\BackgroundTasks\Models\BackgroundTaskStatus;
2524

26-
class BackgroundTaskFullFocusPresenter extends BackgroundTaskPresenter
25+
class BackgroundTaskFullFocus extends BackgroundTask
2726
{
28-
private $injectedView;
29-
3027
private $acceptableWaitTime = false;
3128

32-
public function __construct(BackgroundTaskFullFocusView $view)
29+
public $taskCompleteEvent;
30+
31+
public function __construct()
3332
{
3433
parent::__construct();
3534

36-
$this->injectedView = $view;
35+
$this->taskCompleteEvent = new Event();
3736
}
3837

39-
protected function createView()
38+
protected function getViewClass()
4039
{
41-
return $this->injectedView;
40+
return BackgroundTaskFullFocusView::class;
4241
}
4342

4443
protected function processStartupEvents()
4544
{
4645
parent::processStartupEvents();
4746

48-
$context = new Context();
47+
$context = Application::current()->context();
4948

50-
if (!$context->getIsAjaxRequest()) {
49+
if (!$context->isXhrRequest()) {
5150
if ($this->acceptableWaitTime) {
5251

5352
usleep($this->acceptableWaitTime);
5453

55-
$status = new BackgroundTaskStatus($this->model->BackgroundTaskStatusID);
54+
$status = new BackgroundTaskStatus($this->model->backgroundTaskStatusId);
5655
$status->reload();
5756

5857
if (!$status->isRunning()) {
59-
$this->raiseEvent("TaskComplete", $status->UniqueIdentifier);
58+
$this->taskCompleteEvent->raise($status->UniqueIdentifier);
6059
}
6160
}
6261
}

src/Presenters/BackgroundTaskFullFocusView.php renamed to src/Leaves/BackgroundTaskFullFocusView.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@
1616
* limitations under the License.
1717
*/
1818

19-
namespace Rhubarb\Scaffolds\BackgroundTasks\Presenters;
19+
namespace Rhubarb\Scaffolds\BackgroundTasks\Leaves;
2020

21-
use Rhubarb\Leaf\Views\HtmlView;
21+
use Rhubarb\Leaf\Leaves\LeafDeploymentPackage;
22+
use Rhubarb\Leaf\Views\View;
2223

23-
class BackgroundTaskFullFocusView extends HtmlView
24+
class BackgroundTaskFullFocusView extends View
2425
{
2526
public function getDeploymentPackage()
2627
{
27-
$package = parent::getDeploymentPackage();
28-
$package->resourcesToDeploy[] = __DIR__ . '/BackgroundTaskViewBridge.js';
29-
$package->resourcesToDeploy[] = __DIR__ . '/BackgroundTaskFullFocusViewBridge.js';
30-
31-
return $package;
28+
return new LeafDeploymentPackage(
29+
__DIR__ . '/BackgroundTaskViewBridge.js',
30+
__DIR__ . '/BackgroundTaskFullFocusViewBridge.js' );
3231
}
3332

34-
protected function getClientSideViewBridgeName()
33+
protected function getViewBridgeName()
3534
{
3635
return "BackgroundTaskFullFocusViewBridge";
3736
}

src/Presenters/BackgroundTaskFullFocusViewBridge.js renamed to src/Leaves/BackgroundTaskFullFocusViewBridge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ bridge.prototype.constructor = bridge;
88

99
bridge.prototype.onComplete = function()
1010
{
11-
this.raisePostBackEvent("TaskComplete", this.model.BackgroundTaskStatusID );
11+
this.raisePostBackEvent("taskComplete", this.model.backgroundTaskStatusId );
1212
};
1313

1414
window.rhubarb.viewBridgeClasses.BackgroundTaskFullFocusViewBridge = bridge;

src/Leaves/BackgroundTaskModel.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rhubarb\Scaffolds\BackgroundTasks\Leaves;
4+
5+
use Rhubarb\Crown\Events\Event;
6+
use Rhubarb\Leaf\Leaves\LeafModel;
7+
8+
class BackgroundTaskModel extends LeafModel
9+
{
10+
public $backgroundTaskStatusId;
11+
12+
/**
13+
* @var Event Raised when the view needs to know the task progress
14+
*/
15+
public $getProgressEvent;
16+
17+
public function __construct()
18+
{
19+
parent::__construct();
20+
21+
$this->getProgressEvent = new Event();
22+
}
23+
24+
protected function getExposableModelProperties()
25+
{
26+
$properties = parent::getExposableModelProperties();
27+
$properties[] = "backgroundTaskStatusId";
28+
return $properties;
29+
}
30+
}

src/Presenters/BackgroundTaskProgressPresenter.php renamed to src/Leaves/BackgroundTaskProgress.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
* limitations under the License.
1717
*/
1818

19-
namespace Rhubarb\Scaffolds\BackgroundTasks\Presenters;
19+
namespace Rhubarb\Scaffolds\BackgroundTasks\Leaves;
2020

21-
require_once __DIR__ . '/BackgroundTaskPresenter.php';
21+
require_once __DIR__ . '/BackgroundTask.php';
2222

23-
class BackgroundTaskProgressPresenter extends BackgroundTaskPresenter
23+
class BackgroundTaskProgress extends BackgroundTask
2424
{
25-
protected function createView()
25+
protected function getViewClass()
2626
{
27-
return new BackgroundTaskProgressView();
27+
return BackgroundTaskProgressView::class;
2828
}
2929
}

src/Presenters/BackgroundTaskProgressView.php renamed to src/Leaves/BackgroundTaskProgressView.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616
* limitations under the License.
1717
*/
1818

19-
namespace Rhubarb\Scaffolds\BackgroundTasks\Presenters;
19+
namespace Rhubarb\Scaffolds\BackgroundTasks\Leaves;
2020

21-
use Rhubarb\Leaf\Views\HtmlView;
21+
use Rhubarb\Leaf\Leaves\LeafDeploymentPackage;
22+
use Rhubarb\Leaf\Views\View;
2223
use Rhubarb\Scaffolds\BackgroundTasks\Models\BackgroundTaskStatus;
2324

24-
class BackgroundTaskProgressView extends HtmlView
25+
class BackgroundTaskProgressView extends View
2526
{
27+
/**
28+
* @var BackgroundTaskModel
29+
*/
30+
protected $model;
31+
2632
protected function printViewContent()
2733
{
28-
$backgroundTaskId = $this->getData("BackgroundTaskID");
34+
$backgroundTaskId = $this->model->backgroundTaskStatusId;
2935
$task = new BackgroundTaskStatus($backgroundTaskId);
3036

3137
?>
@@ -38,14 +44,12 @@ protected function printViewContent()
3844

3945
public function getDeploymentPackage()
4046
{
41-
$package = parent::getDeploymentPackage();
42-
$package->resourcesToDeploy[] = __DIR__ . '/BackgroundTaskViewBridge.js';
43-
$package->resourcesToDeploy[] = __DIR__ . '/BackgroundTaskProgressViewBridge.js';
44-
45-
return $package;
47+
return new LeafDeploymentPackage(
48+
__DIR__ . '/BackgroundTaskViewBridge.js',
49+
__DIR__ . '/BackgroundTaskProgressViewBridge.js' );
4650
}
4751

48-
protected function getClientSideViewBridgeName()
52+
protected function getViewBridgeName()
4953
{
5054
return "BackgroundTaskProgressViewBridge";
5155
}
File renamed without changes.

0 commit comments

Comments
 (0)