Skip to content

Commit

Permalink
add abstract controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
killua-eu committed Jun 3, 2024
1 parent 468fdcd commit 88f18e2
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Controllers/AbstractCommon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Glued\Lib\Controllers;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

abstract class AbstractCommon
{
/**
* @var ContainerInterface
*/
protected $c;


/**
* AbstractController constructor. We're passing the whole container to the constructor to be
* able to do stuff like $this->c->db->method(). This is considered bad pracise that makes
* the whole app more memory hungry / less efficient. Dependency injection should be rewritten
* to take advantage of PHP-DI's autowiring.
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->c = $container;
}


/**
* __get is a magic method that allows us to always get the correct property out of the
* container, allowing to write $this->db->method() instead of $this->c->db->method()
* @param string $property Container property
*/
public function __get($property)
{
if ($this->c->get($property)) {
return $this->c->get($property);
}
}


public function getOpenapi(Request $request, Response $response, array $args = []): Response
{
// Directory to look for paths
$path = "{$this->settings['glued']['datapath']}/{$this->settings['glued']['uservice']}/cache" ;
$filesWhitelist = ["openapi.json", "openapi.yaml", "openapi.yml"]; // Potential file names

foreach ($filesWhitelist as $file) {
$fullPath = rtrim($path, '/') . '/' . $file;
if (file_exists($fullPath)) {
$content = file_get_contents($fullPath);
$response->getBody()->write($content);
$contentType = 'application/json';
if (pathinfo($fullPath, PATHINFO_EXTENSION) === 'yaml' || pathinfo($fullPath, PATHINFO_EXTENSION) === 'yml') { $contentType = 'application/x-yaml'; }
return $response->withHeader('Content-Type', $contentType);
}
}
throw new \Exception("OpenAPI specification not found", 404);
}


public function getHealth(Request $request, Response $response, array $args = []): Response {
try {
$check['service'] = basename(__ROOT__);
$check['timestamp'] = microtime();
$check['healthy'] = true;
$check['status']['postgres'] = $this->pg->query("select true as test")->fetch()['test'] ?? false;
$check['status']['auth'] = $_SERVER['X-GLUED-AUTH-UUID'] ?? 'anonymous';
} catch (Exception $e) {
$check['healthy'] = false;
return $response->withJson($check);
}
return $response->withJson($check);
}

}
64 changes: 64 additions & 0 deletions src/Controllers/AbstractIf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Glued\Lib\Controllers;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

abstract class AbstractIf extends AbstractCommon
{
/**
* @var ContainerInterface
*/
protected $c;
protected $deployment;
protected $q;


/**
* AbstractController constructor. We're passing the whole container to the constructor to be
* able to do stuff like $this->c->db->method(). This is considered bad pracise that makes
* the whole app more memory hungry / less efficient. Dependency injection should be rewritten
* to take advantage of PHP-DI's autowiring.
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->c = $container;
$this->deployment = new \Glued\Lib\Sql($this->pg, 'if__deployments');
$this->q = [];
}


protected function getInterface($deploymentUUID, $interfaceConnector)
{
try {
$d = $this->deployment->get($deploymentUUID);
foreach ($d['interfaces'] as $interface) {
if ($interface['connector'] === $interfaceConnector) {
return $interface;
}
};
} catch (\Exception $e) { throw new \Exception("Deployment {$deploymentUUID} not found or data source gone. Available deployments are listed at {$this->settings["glued"]["baseuri"]}{$this->settings["routes"]["be_if_deployments"]["pattern"]}?service=s4s", 500); }
throw new \Exception("Bad interface configuration / interface connector missing in {$deploymentUUID}",500);
}


public function methods(Request $request, Response $response, array $args = []): Response
{
$service = explode("/", (string) $request->getUri()->getPath())[4];
$filteredRoutes = array_filter($this->settings["routes"], function ($route) use ($service) {
return isset($route["service"]) && strpos($route["service"], "if/{$service}") === 0;
});
foreach ($filteredRoutes as $r) {
$res[] = [
'label' => $r['label'],
'dscr' => $r['dscr'],
'uri' => $this->settings['glued']['baseuri'].str_replace('{deployment}', $args['deployment'], $r['pattern'])
];
}
return $response->withJson($res);
}

}

0 comments on commit 88f18e2

Please sign in to comment.