Skip to content

Commit

Permalink
Moved processors into \Swagger\Processing allowing easy registration …
Browse files Browse the repository at this point in the history
…of custom processors.

Closes zircote#183
  • Loading branch information
bfanger committed Apr 27, 2015
1 parent dc31e49 commit fdfc507
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 15 deletions.
77 changes: 77 additions & 0 deletions src/Processing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @license Apache 2.0
*/

namespace Swagger;

use Closure;
use Exception;
use Swagger\Annotations\Swagger;
use Swagger\Processors\AugmentParameter;
use Swagger\Processors\BuildPaths;
use Swagger\Processors\ClassProperties;
use Swagger\Processors\InheritProperties;
use Swagger\Processors\MergeSwagger;

/**
* Registry for the post-processing operations.
*/
class Processing {

/**
* Apply all processors
* @param Swagger $swagger
*/
static function process($swagger) {
foreach (self::processors() as $processor) {
$processor($swagger);
}
}

/**
* @var Closure[]
*/
private static $processors;

/**
* Get direct access to the processors array.
* @return array reference
*/
static function &processors() {
if (!self::$processors) {
// Add default processors.
self::$processors = [
new MergeSwagger(),
new BuildPaths(),
new ClassProperties(),
new InheritProperties(),
new AugmentParameter(),
];
}
return self::$processors;
}

/**
* Register a processor
* @param Closure $processor
*/
static function register($processor) {
array_push(self::processors(), $processor);
}

/**
* Unregister a processor
* @param Closure $processor
*/
static function unregister($processor) {
$processors = &self::processors();
$key = array_search($processor, $processors, true);
if ($key === false) {
throw new Exception('Given processor was not registered');
}
unset($processors[$key]);
}

}
16 changes: 1 addition & 15 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
namespace Swagger;

use Swagger\Annotations\Swagger;
use Swagger\Processors\MergeSwagger;
use Swagger\Processors\BuildPaths;
use Swagger\Processors\ClassProperties;
use Swagger\Processors\InheritProperties;
use Swagger\Processors\AugmentParameter;
use Symfony\Component\Finder\Finder;

/**
Expand All @@ -35,16 +30,7 @@ function scan($directory, $exclude = null) {
// Crawl directory and parse all files
$swagger->crawl($directory, $exclude);
// Post processing
$processors = [
new MergeSwagger(),
new BuildPaths(),
new ClassProperties(),
new InheritProperties(),
new AugmentParameter(),
];
foreach ($processors as $processor) {
$processor($swagger);
}
Processing::process($swagger);
// Validation (Generate notices & warnings)
$swagger->validate();
return $swagger;
Expand Down
30 changes: 30 additions & 0 deletions tests/ProcessingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* @license Apache 2.0
*/

namespace SwaggerTests;

use Swagger\Annotations\Swagger;
use Swagger\Processing;

class ProcessingTest extends SwaggerTestCase {

function testRegister() {
$counter = 0;
$swagger = $this->createSwaggerWithInfo();
Processing::process($swagger);
$this->assertSame(0, $counter);
$countProcessor = function (Swagger $swagger) use (&$counter) {
$counter++;
};
Processing::register($countProcessor);
Processing::process($swagger);
$this->assertSame(1, $counter);
Processing::unregister($countProcessor);
Processing::process($swagger);
$this->assertSame(1, $counter);
}

}

0 comments on commit fdfc507

Please sign in to comment.