Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/vendor
.phpunit.result.cache
/composer.lock
/coverage.clover
19 changes: 11 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
"ext-mbstring" : "*",
"psr/http-message": ">=1.0",
"fatcode/annotations": ">=1.0",
"zendframework/zend-diactoros": "^2.1"
"zendframework/zend-diactoros": ">=2.1",
"nikic/php-parser": ">=4.2"
},
"require-dev": {
"phpunit/phpunit": ">=8.0",
"mockery/mockery": ">=1.2"
"mockery/mockery": ">=1.2",
"vimeo/psalm": ">=3.2",
"squizlabs/php_codesniffer": ">=3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -38,10 +41,10 @@
"FatCode\\Tests\\OpenApi\\": "tests/"
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/fat-code/annotations"
}
]
"scripts": {
"phpunit": "vendor/bin/phpunit --coverage-text",
"phpcs": "vendor/bin/phpcs --standard=PSR12 --warning-severity=0 src",
"phpcsf": "vendor/bin/phpcbf --standard=PSR12 --warning-severity=0 src",
"psalm": "vendor/bin/psalm --show-info=false"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
class Application
{
/**
* @Api\PathItem\Get(
* route="/hello/{name<\w>}",
* @Api\Operation\Get(
* route="/hello/{name:\w}",
* responses=[
* @Api\Response(code=200, schema=@Api\Reference(TextPlain::class))
* ]
Expand Down
32 changes: 0 additions & 32 deletions examples/hello_world.php

This file was deleted.

75 changes: 75 additions & 0 deletions examples/hello_world/hello_world.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php declare(strict_types=1);

namespace App\HelloWorld {

use FatCode\OpenApi\Annotation as Api;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use FatCode\OpenApi\Http\Response;

/**
* Your entry point/front controller.
*
* @Api\Application(
* title = "Your application title",
* version = "1.0.0",
* servers = [
* @Api\Server(
* id = "development",
* port = 8080,
* host = "localhost"
* )
* ]
* )
*/
class Application
{
/**
* @Api\Operation\Get(
* description="Farewell user",
* route="/goodbye/{name}",
* responses=[
* @Api\Response(code=200, schema=@Api\Reference(Greeter::class))
* ]
* )
*/
public function sayGoodbye(ServerRequestInterface $request) : ResponseInterface
{
return new Response(200, new Greeter($request->getAttribute('name')));
}
}

/**
* @Api\Schema(
* title="Greeter schema",
* type="object"
* )
*/
class Greeter
{
/**
* @Api\Property(type="string")
*/
public $name;

public function __construct(string $name)
{
$this->name = $name;
}
}

/**
* @Api\Operation\Get(
* description="Greet user",
* route="/welcome/{name}",
* responses=[
* @Api\Response(code=200, schema=@Api\Reference(Greeter::class))
* ]
* )
*/
function sayHello(ServerRequestInterface $request) : ResponseInterface
{
return new Response(200, new Greeter($request->getAttribute('name')));
}
# Run with `open-api run development`
}
25 changes: 25 additions & 0 deletions examples/multi_directory_project/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace MultiDirectory;

use FatCode\OpenApi\Annotation as Api;

/**
* Main application class.
*
* @Api\Application(
* title = "Your application title",
* version = "1.0.0",
* servers = [
* @Api\Server(
* id = "development",
* port = 8080,
* host = "localhost"
* )
* ]
* )
*/
class Application
{

}
38 changes: 38 additions & 0 deletions examples/multi_namespaces_example/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace App {

use FatCode\OpenApi\Annotation as Api;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use FatCode\OpenApi\Application\OnRequestListener;
use Zend\Diactoros\Response;

/**
* Your entry point/front controller.
*
* @Api\Application(
* title = "Your application title",
* version = "1.0.0",
* servers = [
* @Api\Server(
* id = "development",
* port = 8080,
* host = "localhost"
* )
* ]
* )
*/
class Application implements OnRequestListener
{
public function onRequest(ServerRequestInterface $request): ResponseInterface
{
return new Response('Hello world');
}
}
# Run with `open-api run development`
}

namespace App\Routes {

}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="All Tests">
Expand Down
19 changes: 19 additions & 0 deletions src/Analyzer/AnnotationProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace FatCode\OpenApi\Analyzer;

/**
* Class ProjectAnalyser
* @package FatCode\OpenApi
*/
class AnnotationProcessor
{
public function __construct()
{
}

public function process(Project $project) : void
{

}
}
54 changes: 54 additions & 0 deletions src/Analyzer/Parser/ClassParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

namespace FatCode\OpenApi\Analyzer\Parser;

use FatCode\OpenApi\Analyzer\PhpStream;

use function is_array;

use const T_CLASS;
use const T_NAMESPACE;
use const T_STRING;

class ClassParser implements StreamAnalyzer
{
use NamespaceParser;

private $currentNamespace = '';

/**
* @param PhpStream $stream
* @return Symbol[]
*/
public function analyze(PhpStream $stream) : array
{
$results = [];

foreach ($stream as $index => $token) {
if (is_array($token) && $token[0] === T_NAMESPACE) {
$this->currentNamespace = $this->parseNamespace($stream);
}

if (!is_array($token) || $token[0] !== T_CLASS) {
continue;
}

$results[] = new Symbol(
$this->currentNamespace . '\\' . $this->parseClass($stream),
SymbolType::TYPE_CLASS()
);
}

return $results;
}

private function parseClass(PhpStream $file) : string
{
$file->seekToken(T_STRING);
$className = $file->current()[1];
$file->seekStartOfScope();
$file->skipScope();

return $className;
}
}
60 changes: 60 additions & 0 deletions src/Analyzer/Parser/FunctionParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);

namespace FatCode\OpenApi\Analyzer\Parser;

use FatCode\OpenApi\Analyzer\PhpStream;

use function is_array;

use const T_FUNCTION;
use const T_NAMESPACE;
use const T_STRING;

class FunctionParser implements StreamAnalyzer
{
use NamespaceParser;

private $currentNamespace = '';

/**
* @param PhpStream $stream
* @return Symbol[]
*/
public function analyze(PhpStream $stream) : array
{
$results = [];

foreach ($stream as $index => $token) {
if (is_array($token) && $token[0] === T_NAMESPACE) {
$this->currentNamespace = $this->parseNamespace($stream);
}

if (is_array($token) && $token[0] === T_CLASS) {
$stream->seekStartOfScope();
$stream->skipScope();
continue;
}

if (!is_array($token) || $token[0] !== T_FUNCTION) {
continue;
}

$results[] = new Symbol(
$this->currentNamespace . '\\' . $this->parseFunction($stream),
SymbolType::TYPE_FUNCTION()
);
}

return $results;
}

private function parseFunction(PhpStream $file) : string
{
$file->seekToken(T_STRING);
$className = $file->current()[1];
$file->seekStartOfScope();
$file->skipScope();

return $className;
}
}
30 changes: 30 additions & 0 deletions src/Analyzer/Parser/NamespaceParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace FatCode\OpenApi\Analyzer\Parser;

use FatCode\OpenApi\Exception\ProjectAnalyzerException;
use FatCode\OpenApi\Analyzer\PhpStream;

use function is_array;

trait NamespaceParser
{
protected function parseNamespace(PhpStream $stream) : string
{
$namespace = '';
while ($stream->valid()) {
$stream->next();
$token = $stream->current();

if (is_array($token) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
$namespace .= $token[1];
}

if ($token === ';' || $token === '{') { // End of namespace
return $namespace;
}
}

throw ProjectAnalyzerException::forInvalidNamespace();
}
}
Loading