forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved processors into \Swagger\Processing allowing easy registration …
…of custom processors. Closes zircote#183
- Loading branch information
Showing
3 changed files
with
108 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |