Skip to content

Commit

Permalink
Improvement: Can conf your autoload.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Servuc committed Nov 24, 2020
1 parent a612f3c commit f754492
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Required parameters :

Optional parameter :

- `--tag` : Can be used more that one time. Define tag use filter component and route for partial swagger. If no `--tag` set, no filtering will be made.
- `--tag` : Can be used more that one time. Define tag use filter component and route for partial swagger. If no `--tag` set, no filtering will be made;
- `--autoload` : Path to `autoload.php` in vendor directory.

### Configuration file

Expand All @@ -57,6 +58,7 @@ Your `OA-Doc-Parser.json` should be define as :

{
"composer": "./path/to/your/composer.json",
"autoload": "./path/to/your/autoload.php",
"swagger": {
"header": "./path/to/your/swagger-header.yml",
"output": "./path/to/your/output-swagger.yml",
Expand All @@ -75,7 +77,8 @@ Required parameters :

Optional parameter :

- `swagger.partial` : String array, define tag use filter component and route for partial swagger. If no `swagger.partial` set, no filtering will be made.
- `swagger.partial` : String array, define tag use filter component and route for partial swagger. If no `swagger.partial` set, no filtering will be made;
- `autoload` : Path to `autoload.php` in vendor directory.

### Swagger header

Expand Down
33 changes: 30 additions & 3 deletions src/ArgParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ class ArgParser

public const ARG_KEY_COMPOSER = "--composer";

public const ARG_KEY_AUTOLOAD = "--autoload";

public const ARG_KEY_SWAGGER_HEADER = "--swagger-header";

public const ARG_KEY_SWAGGER_OUTPUT = "--swagger-output";

public static function isDefinedArgs(string $args) : bool
{
return in_array($args, array( self::ARG_KEY_CONF, self::ARG_KEY_TAG, self::ARG_KEY_COMPOSER, self::ARG_KEY_SWAGGER_HEADER, self::ARG_KEY_SWAGGER_OUTPUT ));
return in_array($args, array( self::ARG_KEY_AUTOLOAD, self::ARG_KEY_CONF, self::ARG_KEY_TAG, self::ARG_KEY_COMPOSER, self::ARG_KEY_SWAGGER_HEADER, self::ARG_KEY_SWAGGER_OUTPUT ));
}

/**
Expand All @@ -39,6 +41,11 @@ public static function isDefinedArgs(string $args) : bool
*/
private ?string $swaggerOutputPath;

/**
* Path of output swagger path. Full from / root
*/
private ?string $autoloadPath;

/**
* Tags required for parsing
*/
Expand All @@ -54,6 +61,7 @@ private function __construct()
$this->swaggerHeaderPath = null;
$this->swaggerOutputPath = null;
$this->confFilePath = null;
$this->autoloadPath = null;
$this->program = null;
$this->psr4 = array();
$this->tags = array();
Expand All @@ -79,6 +87,12 @@ public function getSwaggerOutputPath() : ?string
return $this->swaggerOutputPath;
}

public function getAutoloadPath() : ?string
{
return $this->autoloadPath;
}


public function getPSR4() : array
{
return $this->psr4;
Expand Down Expand Up @@ -137,6 +151,10 @@ public function hasError() : ?string
if (isset($myConfFileContent['swagger']['tags'])) {
$this->tags = $myConfFileContent['swagger']['tags'];
}

if( isset($myConfFileContent['autoload']) && is_string($myConfFileContent['autoload']) ) {
$this->autoloadPath = $myConfFileContent['autoload'];
}
}

//composer.json is valid ?
Expand Down Expand Up @@ -175,6 +193,10 @@ public function hasError() : ?string
return 'Can\'t write in : ' . $this->getSwaggerOutputPath();
}

if( $this->getAutoloadPath() !== null && ! is_file( $this->getAutoloadPath() ) ) {
return 'Can\'t read autoload in : ' . $this->getAutoloadPath();
}

return null;
}

Expand All @@ -184,7 +206,7 @@ public function hasError() : ?string
*/
public function getUsage(string $args0) : string
{
return "Usage : \n" . $args0 . " " . self::ARG_KEY_COMPOSER . " ./path/to/your/composer.json " . self::ARG_KEY_SWAGGER_HEADER . " ./path/to/your/swagger-header.yml " . self::ARG_KEY_SWAGGER_OUTPUT . " ./path/to/your/output-swagger.yml\n" . $args0 . " " . self::ARG_KEY_CONF . " ./path/to/your/OA_Doc_Parser.json";
return "Usage : \n" . $args0 . " " . self::ARG_KEY_COMPOSER . " ./path/to/your/composer.json " . self::ARG_KEY_SWAGGER_HEADER . " ./path/to/your/swagger-header.yml " . self::ARG_KEY_SWAGGER_OUTPUT . " ./path/to/your/output-swagger.yml " . self::ARG_KEY_AUTOLOAD . " ./path/to/your/autoload.php\n" . $args0 . " " . self::ARG_KEY_CONF . " ./path/to/your/OA_Doc_Parser.json";
}

/**
Expand Down Expand Up @@ -217,7 +239,12 @@ public static function extractArgs(array $args) : ArgParser
if ($i < $myArgsCount && ! self::isDefinedArgs($args[$i])) {
$myArgParser->confFilePath = $args[$i];
}
} elseif ($args[$i] === self::ARG_KEY_TAG) {
} elseif ($args[$i] === self::ARG_KEY_AUTOLOAD) {
$i++;
if ($i < $myArgsCount && ! self::isDefinedArgs($args[$i])) {
$myArgParser->autoloadPath = $args[$i];
}
}elseif ($args[$i] === self::ARG_KEY_TAG) {
$i++;
if ($i < $myArgsCount && ! self::isDefinedArgs($args[$i])) {
$myArgParser->tags[] = $args[$i];
Expand Down
26 changes: 15 additions & 11 deletions src/AutoloadReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ public function getClassLoader() //@phpstan-ignore-line
return $this->classLoader;
}

public static function load() : ?AutoloadReader
public static function load( ?string $autoloadPath = null ) : ?AutoloadReader
{
$myPaths = array(
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../autoload.php',
__DIR__ . '/../autoload.php',
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/autoload.php'
);

if( $autoloadPath !== null ) {
$myPaths[] = $autoloadPath;
}

//Autoload.php OK ?
$myAutoloadReader = null;
foreach (
array(
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../autoload.php',
__DIR__ . '/../autoload.php',
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/autoload.php'
)
as $file) {
foreach ( $myPaths as $file) {
if (is_file($file)) {
$myAutoloadReader = new AutoloadReader();
$myAutoloadReader->classLoaderFile = $file;
Expand Down
1 change: 1 addition & 0 deletions tests/ArgParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function testBasic(): void
self::assertNull( $myArgParser->getConfFilePath() );
self::assertNull( $myArgParser->getSwaggerHeaderPath() );
self::assertNull( $myArgParser->getSwaggerOutputPath() );
self::assertNull( $myArgParser->getAutoloadPath() );
self::assertEmpty( $myArgParser->getPSR4() );
self::assertEmpty( $myArgParser->getTags() );
self::assertFalse( $myArgParser->isArgsValid() );
Expand Down

0 comments on commit f754492

Please sign in to comment.