promise refactoring++
authorMichael Wallner <mike@php.net>
Tue, 9 Jun 2015 15:57:04 +0000 (17:57 +0200)
committerMichael Wallner <mike@php.net>
Tue, 9 Jun 2015 15:57:04 +0000 (17:57 +0200)
app/Controller/Github/RepoHook.php
app/Github/API/Hooks/CreateHook.php
app/Github/API/Hooks/DeleteHook.php
app/Github/API/Hooks/UpdateHook.php
app/Github/API/Releases/CreateRelease.php
app/Github/API/Releases/CreateReleaseAsset.php
app/Github/API/Releases/PublishRelease.php
app/views/github/repo.phtml

index 39f6f1bb7acab299442e92f0ca50fc30c3c107bf..14d3d69da48fa20e0b13b40f613f7e9938532009 100644 (file)
@@ -35,45 +35,52 @@ class RepoHook extends Github
        
        function addHook($owner, $repo) {
                $hook_conf = $this->app->getRequest()->getForm();
-               $this->github->createRepoHook("$owner/$repo", $hook_conffunction($hook) use($owner, $repo) {
+               $this->github->createRepoHook("$owner/$repo", $hook_conf)->then(function($hook) use($owner, $repo) {
                        $call = new ListHooks($this->github, ["repo" => "$owner/$repo", "fresh" => true]);
-                       $call(function($hooks, $links) use($owner, $repo, $call) {
-                               $call->saveToCache([$hooks, $links]);
+                       $this->github->queue($call)->then(function() use($owner, $repo) {
                                $this->redirectBack("$owner/$repo");
                        });
-               })->send();
+               });
+               $this->github->drain();
        }
        
        function updateHook($owner, $repo) {
-               $this->github->readRepo("$owner/$repo", function($repo) {
-                       $call = $this->github->listHooks($repo->full_name, function($hooks, $links) use($repo, &$call) {
-                               $repo->hooks = $hooks;
+               $this->github->readRepo("$owner/$repo")->then(function($result) {
+                       list($repo) = $result;
+                       $call = new ListHooks($this->github, ["repo" => $repo->full_name]);
+                       $this->github->queue($call)->then(function($result) use($repo, $call) {
+                               list($repo->hooks) = $result;
                                if (($hook = $this->github->checkRepoHook($repo))) {
                                        $hook_conf = $this->app->getRequest()->getForm();
-                                       $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf, function($changed_hook) use($repo, $hook, $hooks, $links, &$call) {
+                                       $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf)->then(function($hook_result) use($repo, $hook, $result, $call) {
+                                               list($changed_hook) = $hook_result;
                                                foreach ($changed_hook as $key => $val) {
                                                        $hook->$key = $val;
                                                }
-                                               $call->saveToCache([$hooks, $links]);
+                                               $call->saveToCache($result);
                                                $this->redirectBack($repo->full_name);
                                        });
                                }
                        });
-               })->send();
+               });
+               $this->github->drain();
        }
        
        function delHook($owner, $repo) {
-               $this->github->readRepo("$owner/$repo", function($repo) {
-                       $call = $this->github->listHooks($repo->full_name, function($hooks) use($repo, &$call) {
-                               $repo->hooks = $hooks;
+               $this->github->readRepo("$owner/$repo")->then(function($result) {
+                       list($repo) = $result;
+                       $call = new ListHooks($this->github, ["repo" => $repo->full_name]);
+                       $this->github->queue($call)->then(function($result) use($repo, $call) {
+                               list($repo->hooks) = $result;
                                if (($hook = $this->github->checkRepoHook($repo))) {
-                                       $this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo, &$call) {
+                                       $this->github->deleteRepoHook($repo->full_name, $hook->id)->then(function() use($repo, $call) {
                                                $call->dropFromCache();
                                                $this->redirectBack($repo->full_name);
                                        });
                                }
                        });
-               })->send();
+               });
+               $this->github->drain();
        }
        
        function redirectBack($repo) {
index 146929b6f3372f22953fb36a8fdef37a7873c7df..0d36ad66a621c32475b264bc2736f679e5dd8cd1 100644 (file)
@@ -5,10 +5,11 @@ namespace app\Github\API\Hooks;
 use app\Github\API\Call;
 use app\Github\Exception\RequestException;
 use http\Client\Request;
+use http\Client\Response;
 
 class CreateHook extends Call
 {
-       function enqueue(callable $callback) {
+       function request() {
                $url = $this->url->mod("./repos/". $this->args["repo"] ."/hooks");
                $request = new Request("POST", $url, [
                        "Authorization" => "token " . $this->api->getToken(),
@@ -37,13 +38,13 @@ class CreateHook extends Call
                        ]
                ]));
                
-               $this->api->getClient()->enqueue($request, function($response) use($callback) {
-                       if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
-                               throw new RequestException($response);
-                       }
-                       $this->result = [$json];
-                       $callback($json);
-                       return true;
-               });
+               return $request;
+       }
+       
+       function response(Response $response) {
+               if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
+                       throw new RequestException($response);
+               }
+               return [$json];
        }
 }
index a94f2855c086bfc7d28ec92629c03db85478beb6..3635f6e65ae274f0a9fcab727483ef0852f160a3 100644 (file)
@@ -5,21 +5,22 @@ namespace app\Github\API\Hooks;
 use app\Github\API\Call;
 use app\Github\Exception\RequestException;
 use http\Client\Request;
+use http\Client\Response;
 
 class DeleteHook extends Call
 {
-       function enqueue(callable $callback) {
+       function request() {
                $url = $this->url->mod(uri_template("./repos/{+repo}/hooks{/id}", $this->args));
                $request = new Request("DELETE", $url, [
                        "Authorization" => "token " . $this->api->getToken(),
                        "Accept" => $this->config->api->accept,
                ]);
-               $this->api->getClient()->enqueue($request, function($response) use($callback) {
-                       if ($response->getResponseCode() >= 400) {
-                               throw new RequestException($response);
-                       }
-                       $callback();
-                       return true;
-               });
+               return $request;
+       }
+       
+       function response(Response $response) {
+               if ($response->getResponseCode() >= 400) {
+                       throw new RequestException($response);
+               }
        }
 }
index 2d994b592ec88703feebc4b3b16aa5ff2c9d4afe..f03010770be115da3358b1d8e6ce0212b9ece5a7 100644 (file)
@@ -2,11 +2,16 @@
 
 namespace app\Github\API\Hooks;
 
-class UpdateHook extends \app\Github\API\Call
+use app\Github\API\Call;
+use app\Github\Exception\RequestException;
+use http\Client\Request;
+use http\Client\Response;
+
+class UpdateHook extends Call
 {
-       function enqueue(callable $callback) {
+       function request() {
                $url = $this->url->mod(uri_template("./repos/{+repo}/hooks{/id}", $this->args));
-               $request = new \http\Client\Request("PATCH", $url, [
+               $request = new Request("PATCH", $url, [
                        "Authorization" => "token ". $this->api->getToken(),
                        "Accept" => $this->config->api->accept,
                        "Content-Type" => "application/json",
@@ -29,13 +34,13 @@ class UpdateHook extends \app\Github\API\Call
                ];
 
                $request->getBody()->append(json_encode(compact("events", "config")));
-               $this->api->getClient()->enqueue($request, function($response) use($callback) {
-                       if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
-                               throw new \app\Github\Exception\RequestException($response);
-                       }
-                       $this->result = [$json];
-                       $callback($json);
-                       return true;
-               });
+               return $request;
+       }
+       
+       function response(Response $response) {
+               if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
+                       throw new RequestException($response);
+               }
+               return [$json];
        }
 }
index 1cb22221fb1df584989573acbb1d5d83820e985f..c7b1a4a76982370092306757969215ab8e7bac18 100644 (file)
@@ -5,10 +5,11 @@ namespace app\Github\API\Releases;
 use app\Github\API\Call;
 use app\Github\Exception\RequestException;
 use http\Client\Request;
+use http\Client\Response;
 
 class CreateRelease extends Call
 {
-       function enqueue(callable $callback) {
+       function request() {
                $url = $this->url->mod("/repos/". $this->args["repo"] ."/releases");
                $request = new Request("POST", $url, [
                        "Authorization" => "token ". $this->api->getToken(),
@@ -19,13 +20,13 @@ class CreateRelease extends Call
                        "tag_name" => $this->args["tag"],
                        "draft" => true,
                ]));
-               $this->api->getClient()->enqueue($request, function($response) use($callback) {
-                       if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
-                               throw new RequestException($response);
-                       }
-                       $this->result = [$json];
-                       $callback($json);
-                       return true;
-               });
+               return $request;
+       }
+       
+       function response(Response $response) {
+               if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
+                       throw new RequestException($response);
+               }
+               return [$json];
        }
 }
index 88b5a493bb8a1a4482b680a3a55b23902d8875ee..ce1ad360085e8b53c0fc833ebe2521fc5b248045 100644 (file)
@@ -5,24 +5,25 @@ namespace app\Github\API\Releases;
 use app\Github\API\Call;
 use app\Github\Exception\RequestException;
 use http\Client\Request;
+use http\Client\Response;
 use http\Message\Body;
 
 class CreateReleaseAsset extends Call
 {
-       function enqueue(callable $callback) {
+       function request() {
                $body = new Body(fopen($this->args["asset"], "rb"));
                $request = new Request("POST", $this->args["url"], [
                        "Authorization" => "token ". $this->api->getToken(),
                        "Accept" => $this->config->api->accept,
                        "Content-Type" => $this->args["type"],
                ], $body);
-               $this->api->getClient()->enqueue($request, function($response) use($callback) {
-                       if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
-                               throw new RequestException($response);
-                       }
-                       $this->result = [$json];
-                       $callback($json);
-                       return true;
-               });
+               return $request;
+       }
+       
+       function response(Response $response) {
+               if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
+                       throw new RequestException($response);
+               }
+               return [$json];
        }
 }
index 39c5169ed9eebfa5f1b9ba5108e74335b175578e..d4803762a3b891006f6bf34d8f029d4108258b11 100644 (file)
@@ -5,10 +5,11 @@ namespace app\Github\API\Releases;
 use app\Github\API\Call;
 use app\Github\Exception\RequestException;
 use http\Client\Request;
+use http\Client\Response;
 
 class PublishRelease extends Call
 {
-       function enqueue(callable $callback) {
+       function request() {
                $url = $this->url->mod(uri_template("./repos/{+repo}/releases{/id}", $this->args));
                $request = new Request("PATCH", $url, [
                        "Authorization" => "token ". $this->api->getToken(),
@@ -19,13 +20,13 @@ class PublishRelease extends Call
                        "draft" => false,
                        "tag_name" => $this->args["tag"],
                ]));
-               $this->api->getClient()->enqueue($request, function($response) use($callback) {
-                       if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
-                               throw new RequestException($response);
-                       }
-                       $this->result = [$json];
-                       $callback($json);
-                       return true;
-               });
+               return $request;
+       }
+       
+       function response(Response $response) {
+               if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
+                       throw new RequestException($response);
+               }
+               return [$json];
        }
 }
\ No newline at end of file
index 9d5f7eb62d49211641093a617f39c853e2045e9a..fff952d75cdafab28b194e9a7fdcef3f955e6cfe 100644 (file)
                <?php if (!empty($v->release)) : ?>
                        <p><?= $this->md($v->release->body) ?></p>
                <?php endif; ?>
-               <ul class="list-inline">
-                       <li>
-                               <span class="octicon octicon-tag" title="Tag"></span>
-                               <span class="label label-default"><?= $this->e($v->tag->name) ?></span>
-                       </li>
+                               <ul class="list-inline">
+                                       <li>
+                                               <span class="octicon octicon-tag" title="Tag"></span>
+                                               <span class="label label-default"><?= $this->e($v->tag->name) ?></span>
+                                       </li>
 
-                       <?php if (!empty($v->release)) : ?>
-                       <li>
-                               <span class="octicon octicon-git-branch" title="Branch"></span>
-                               <span class="label label-default"><?= $this->e($v->release->target_commitish) ?></span>
-                       </li>
-                       <li>
-                               <span class="octicon octicon-clock" title="Date"></span>
-                               <span class="label label-default">
-                                       <time datetime="<?= $v->release->published_at ?>">
-                                               <?= $this->utc($v->release->published_at)->format("Y-m-d H:i T") ?>
-                                       </time>
-                               </span>
-                       <?php endif; ?>
-               </ul>
+                                       <?php if (!empty($v->release)) : ?>
+                                       <li>
+                                               <span class="octicon octicon-git-branch" title="Branch"></span>
+                                               <span class="label label-default"><?= $this->e($v->release->target_commitish) ?></span>
+                                       </li>
+                                       <li>
+                                               <span class="octicon octicon-clock" title="Date"></span>
+                                               <span class="label label-default">
+                                                       <time datetime="<?= $v->release->published_at ?>">
+                                                               <?= $this->utc($v->release->published_at)->format("Y-m-d H:i T") ?>
+                                                       </time>
+                                               </span>
+                                       <?php endif; ?>
+                               </ul>
                        </div>
                        <div class="col-sm-4">
-                       <ul class="list-inline pull-right">
                        <?php if (!empty($v->release->assets)) : ?>
-                               <?php foreach ($v->release->assets as $asset) : ?>
-                               <?php if (fnmatch("*.ext.phar", $asset->name)) : ?>
-                               <li>
-                                       <a class="btn btn-success" href="<?= $this->e($asset->browser_download_url) ?>">
-                                               <span class="octicon octicon-package"></span>
-                                               <?= $this->e($asset->name) ?>
-                                       </a>
-                               </li>
-                               <?php endif; ?>
-                               <?php endforeach; ?>
+                               <ul class="list-inline pull-right">
+                                       <?php foreach ($v->release->assets as $asset) : ?>
+                                       <?php if (fnmatch("*.ext.phar", $asset->name)) : ?>
+                                       <li>
+                                               <a class="btn btn-success" href="<?= $this->e($asset->browser_download_url) ?>">
+                                                       <span class="octicon octicon-package"></span>
+                                                       <?= $this->e($asset->name) ?>
+                                               </a>
+                                       </li>
+                                       <?php endif; ?>
+                                       <?php endforeach; ?>
+                               </ul>
                        <?php else: ?>
-                               <form class="form-inline" method="post" action="<?= $baseUrl->mod("./github/repo/". $repo->full_name ."/release") ?>">
+                               <form class="form-inline pull-right" method="post" action="<?= $baseUrl->mod("./github/repo/". $repo->full_name ."/release") ?>">
                                        <input type="hidden" name="tag" value="<?= $this->e($v->tag->name) ?>">
                                        <div class="checkbox">
                                                <label for="hook-zend">
                                        </button>
                                </form>
                        <?php endif; ?>
-                       </ul>
+                       
                        </div>
                </div>
        </div>