add demo code for Symfony
authorgggeek <giunta.gaetano@gmail.com>
Tue, 31 Jan 2023 18:50:53 +0000 (18:50 +0000)
committergggeek <giunta.gaetano@gmail.com>
Tue, 31 Jan 2023 18:50:53 +0000 (18:50 +0000)
demo/client/symfony/ClientController.php [new file with mode: 0644]
demo/client/symfony/services.yaml [new file with mode: 0644]
demo/server/symfony/ServerController.php [new file with mode: 0644]
demo/server/symfony/services.yaml [new file with mode: 0644]

diff --git a/demo/client/symfony/ClientController.php b/demo/client/symfony/ClientController.php
new file mode 100644 (file)
index 0000000..b42ed0d
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+namespace App\Controller;
+
+use PhpXmlRpc\Client;
+use PhpXmlRpc\Request;
+use PhpXmlRpc\Value;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+use Symfony\Component\Routing\Annotation\Route;
+
+class ClientController extends AbstractController
+{
+    protected $client;
+
+    public function __construct(Client $client)
+    {
+        $this->client = $client;
+    }
+
+    #[Route('/getStateName/{stateNo}', name: 'getstatename', methods: ['GET'])]
+    public function getStateName(int $stateNo): Response
+    {
+        $response = $this->client->send(new Request('examples.getStateName', [
+            new Value($stateNo, Value::$xmlrpcInt)
+        ]));
+        if ($response->faultCode()) {
+            throw new HttpException(502, $response->faultString());
+        } else {
+            return new Response("<html><body>State number $stateNo is: " . $response->value()->scalarVal() . '</body></html>');
+        }
+    }
+}
diff --git a/demo/client/symfony/services.yaml b/demo/client/symfony/services.yaml
new file mode 100644 (file)
index 0000000..f97690e
--- /dev/null
@@ -0,0 +1,17 @@
+parameters:
+    xml_rpc_server: https://gggeek.altervista.org/sw/xmlrpc/demo/server/server.php
+    xml_rpc_client_options: []
+
+services:
+    # explicitly configure the services where autowiring is impossible
+
+    App\Service\XmlRpcClient:
+        class: PhpXmlRpc\Client
+        arguments:
+            - '%xml_rpc_server%'
+        calls:
+            - setOptions: ['%xml_rpc_client_options%']
+
+    App\Controller\ClientController:
+        arguments:
+            $client: '@App\Service\XmlRpcClient'
diff --git a/demo/server/symfony/ServerController.php b/demo/server/symfony/ServerController.php
new file mode 100644 (file)
index 0000000..80a0c8a
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Controller;
+
+use PhpXmlRpc\Server;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Annotation\Route;
+
+class ServerController extends AbstractController
+{
+    protected $server;
+
+    public function __construct(Server $server)
+    {
+        $this->server = $server;
+    }
+
+    # This single method serves ALL the xml-rpc requests.
+    # The configuration for which xml-rpc methods exist and how they are handled is carried out in the Server service
+    #[Route('/xmlrpc', name: 'xml_rpc', methods: ['POST'])]
+    public function serve(): Response
+    {
+        $xmlrpcResponse = $this->server->service(null, true);
+        $response = new Response($xmlrpcResponse, 200, ['Content-Type' => 'text/xml']);
+        // there should be no need to avoid caching since this is only accessed via POST
+        return $response;
+    }
+}
diff --git a/demo/server/symfony/services.yaml b/demo/server/symfony/services.yaml
new file mode 100644 (file)
index 0000000..79e7010
--- /dev/null
@@ -0,0 +1,31 @@
+parameters:
+    xml_rpc_server_options: []
+
+services:
+    # explicitly configure the services
+
+    App\Service\CommentManager:
+        class: CommentManager
+
+    App\Service\XmlRpcServer:
+        class: PhpXmlRpc\Server
+        arguments:
+            -
+                # the dispatch map can not be defined using a parameter because it contains references to a service
+                discuss.addComment:
+                    function: ['@App\Service\CommentManager', 'addComment'],
+                    signature:
+                        - ['int', 'string', 'string', 'string']
+                    docstring: Adds a comment to an item. The first parameter is the item ID, the second the name of the commenter, and the third is the comment itself. Returns the number of comments against that ID.
+                discuss.getComments:
+                    function: ['@App\Service\CommentManager', 'getComments'],
+                    signature:
+                        - [ 'array', 'string' ]
+                    docstring: Returns an array of comments for a given ID, which is the sole argument. Each array item is a struct containing name and comment text.
+            - false
+        calls:
+            - setOptions: ['%xml_rpc_server_options%']
+
+    App\Controller\ServerController:
+        arguments:
+            $server: '@App\Service\XmlRpcServer'