From be8b4f3adeda328697d0028df59d04b3d80e00b3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaime=20Pe=CC=81rez=20Crespo?= <jaime.perez@uninett.no>
Date: Wed, 17 Oct 2018 15:34:18 +0200
Subject: [PATCH] Add a type of Response that allows us to run a callback.

This allows us to use the router even with old code that won't return an actual Response object, but perform a redirection or print directly to the standard output.
---
 lib/SimpleSAML/HTTP/RunnableResponse.php | 69 ++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 lib/SimpleSAML/HTTP/RunnableResponse.php

diff --git a/lib/SimpleSAML/HTTP/RunnableResponse.php b/lib/SimpleSAML/HTTP/RunnableResponse.php
new file mode 100644
index 000000000..ab9fc6c5f
--- /dev/null
+++ b/lib/SimpleSAML/HTTP/RunnableResponse.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace SimpleSAML\HTTP;
+
+use Symfony\Component\HttpFoundation\Response;
+
+/**
+ * Class modelling a response that consists on running some function.
+ *
+ * This is a helper class that allows us to have the new and the old architecture coexist. This way, classes and files
+ * that aren't PSR-7-aware can still be plugged into a PSR-7-compatible environment.
+ *
+ * @package SimpleSAML
+ */
+class RunnableResponse extends Response
+{
+    /** @var array */
+    protected $arguments;
+
+    /** @var callable */
+    protected $callable;
+
+
+    /**
+     * RunnableResponse constructor.
+     *
+     * @param callable $callable A callable that we should run as part of this response.
+     * @param array $args An array of arguments to be passed to the callable. Note that each element of the array
+     */
+    public function __construct(callable $callable, $args = [])
+    {
+        $this->arguments = $args;
+        $this->callable = $callable;
+        parent::__construct();
+    }
+
+
+    /**
+     * Get the callable for this response.
+     *
+     * @return callable
+     */
+    public function getCallable()
+    {
+        return $this->callable;
+    }
+
+
+    /**
+     * Get the arguments to the callable.
+     *
+     * @return array
+     */
+    public function getArguments()
+    {
+        return $this->arguments;
+    }
+
+
+    /**
+     * "Send" this response by actually running the callable.
+     *
+     * @return mixed
+     */
+    public function send()
+    {
+        return call_user_func_array($this->callable, $this->arguments);
+    }
+}
-- 
GitLab