fork download
  1. <?php
  2. class Task
  3. {
  4. protected $follow_up = [];
  5. protected $task_callback;
  6.  
  7. public function __construct($task_callback)
  8. {
  9. $this->task_callback = $task_callback;
  10. }
  11.  
  12. public function addFollowUp(Task $follow_up)
  13. {
  14. $this->follow_up[] = $follow_up;
  15. }
  16.  
  17. public function complete()
  18. {
  19. foreach($this->follow_up as $runnable) {
  20. $runnable->run();
  21. }
  22. }
  23.  
  24. public function run()
  25. {
  26. $callback = $this->task_callback;
  27.  
  28. $callback($this);
  29. }
  30. }
  31.  
  32.  
  33.  
  34. $provided_task_scheduler_from_somewhere = function()
  35. {
  36. $tasks = [];
  37.  
  38. $global_message_thing = 'failed';
  39.  
  40. $second_global_message_thing = 'failed';
  41.  
  42. $task1 = new Task(function (Task $runner)
  43. {
  44. $something_in_closure = function() use ($runner) {
  45. echo "running task one\n";
  46. $runner->complete();
  47. };
  48. $something_in_closure();
  49. });
  50.  
  51. /**
  52. * use $global_message_thing as reference so we can manipulate it
  53. * This will make sure that the follow up on this one knows the status of what happened here
  54. */
  55. $second_follow_up = new Task(function(Task $runner) use (&$global_message_thing)
  56. {
  57. echo "second follow up on task one.\n";
  58. $global_message_thing = "success";
  59. $runner->complete();
  60. });
  61.  
  62. /**
  63. * Just doing things in random order to show that order doesn't really matter with a task scheduler
  64. * just the follow ups
  65. */
  66. $tasks[] = $task1;
  67.  
  68. $tasks[] = new Task(function(Task $runner)
  69. {
  70. echo "running task 2\n";
  71. $runner->complete();
  72. });
  73.  
  74. $task1->addFollowUp(new Task(function(Task $runner)
  75. {
  76. echo "follow up on task one.\n";
  77. $runner->complete();
  78. }));
  79.  
  80. $task1->addFollowUp($second_follow_up);
  81.  
  82. /**
  83. * Adding the references to our "status" trackers here to know what to print
  84. * One will still be on failed because we did nothing with it. this way we know it works properly
  85. * as a control.
  86. */
  87. $second_follow_up->addFollowUp(new Task(function(Task $runner) use (&$global_message_thing, &$second_global_message_thing) {
  88. if($global_message_thing === "success") {
  89. echo "follow up on the second follow up, three layers now, w00007!\n";
  90. }
  91. if($second_global_message_thing === "success") {
  92. echo "you don't see this\n";
  93. }
  94. $runner->complete();
  95. }));
  96. return $tasks;
  97. };
  98. /**
  99.  * Normally you'd use some aggretating function to build up your tasks
  100.  * list or a collection of classes. I simulated that here with this callback function.
  101.  */
  102. $tasks = $provided_task_scheduler_from_somewhere();
  103.  
  104. foreach($tasks as $task) {
  105. $task->run();
  106. }
Success #stdin #stdout 0.02s 24308KB
stdin
Standard input is empty
stdout
running task one
follow up on task one.
second follow up on task one.
follow up on the second follow up, three layers now, w00007!
running task 2