├── .gitignore ├── oai.png ├── assets ├── .DS_Store └── image.png ├── config └── openapi.php ├── src ├── OpenApiServiceProvider.php └── Console │ └── CreateOpenApiContractCommand.php ├── composer.json ├── LICENSE ├── README.md └── CODE_OF_CONDUCT.md /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | /.idea -------------------------------------------------------------------------------- /oai.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PrimitiveSocial/openapi-initializer/HEAD/oai.png -------------------------------------------------------------------------------- /assets/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PrimitiveSocial/openapi-initializer/HEAD/assets/.DS_Store -------------------------------------------------------------------------------- /assets/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PrimitiveSocial/openapi-initializer/HEAD/assets/image.png -------------------------------------------------------------------------------- /config/openapi.php: -------------------------------------------------------------------------------- 1 | config('app.url'), 5 | 'url_description' => 'Local url for testing', 6 | 'contract_directory' => 'contract', 7 | ]; -------------------------------------------------------------------------------- /src/OpenApiServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 13 | $this->publishes([ 14 | __DIR__.'/../config/openapi.php' => config_path('openapi.php'), 15 | ], 'config'); 16 | 17 | $this->commands([ 18 | CreateOpenApiContractCommand::class 19 | ]); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "primitive/openapi-initializer", 3 | "description": "A command for Laravel to quickly scaffold an OpenAPI Spec file", 4 | "type": "library", 5 | "require": { 6 | "php": ">=7.0", 7 | "symfony/yaml": "^5.1", 8 | "orchestra/testbench": "^4.0||^5.0||^6.3" 9 | }, 10 | "require-dev": { 11 | "phpunit/phpunit": "^9.4" 12 | }, 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "matt trask", 17 | "email": "matt@leadwithprimitive.com" 18 | } 19 | ], 20 | "autoload": { 21 | "psr-4": { 22 | "Primitive\\": "src/" 23 | } 24 | }, 25 | "extra": { 26 | "laravel": { 27 | "providers": [ 28 | "Primitive\\OpenApiServiceProvider" 29 | ] 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Primitive. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # openapi-initializer 2 | 3 | ![logo](https://github.com/PrimitiveSocial/openapi-initializer/blob/main/oai.png) 4 | 5 | Straight forward Laravel command to scaffold an OpenAPI spec file 6 | 7 | Scaffold an OpenAPI spec file in minutes. 8 | 9 | ## OpenAPI 10 | 11 | **OpenAPI** Default Version: 3.1.0 12 | 13 | Read more about the current OpenAPI Version [here](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md) 14 | 15 | 16 | ## Requirements 17 | 18 | **Laravel** versions 6.x, 7.x, and 8.x. 19 | 20 | **PHP** 7.0 or greater. 21 | 22 | ## Install 23 | ``` 24 | composer require primitive/openapi-initializer 25 | ``` 26 | 27 | ### Usage 28 | 29 | To use the command, you need to run 30 | 31 | ``` 32 | php artisan openapi:create 33 | ``` 34 | 35 | From there, prompts will step through the process with you, asking questions to fill in various bits of information needed to make the OpenAPI specification conform to the specification standard. 36 | 37 | If you do not run 38 | 39 | ``` 40 | php artisan vendor:publish --vendor=Primitive\OpenApiServiceProvider 41 | ``` 42 | 43 | prior to running the Artisan command, the command itself will run the `vendor:publish` command as well as caching the config so we can use it in the command. We tried to be thoughtful when writing this command because we want to create an enjoyable developer experience. 44 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at matt@leadwithprimitive.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /src/Console/CreateOpenApiContractCommand.php: -------------------------------------------------------------------------------- 1 | info('Creating your OpenAPI Contract'); 19 | 20 | if (!file_exists(config_path('openapi.php'))) { 21 | $publishConfig = $this->ask('You need to publish the config first. Would you like to do that?', 'yes'); 22 | 23 | if ($publishConfig) { 24 | $this->call('vendor:publish', ['--provider' => 'Primitive\OpenApiServiceProvider']); 25 | $this->info('Lets cache your configs'); 26 | $this->call('config:cache'); 27 | } 28 | } 29 | 30 | $oasVersion = $this->ask('What version of OpenAPI are you using?', '3.1.0'); 31 | $appName = $this->ask('What app are you working on?'); 32 | $developerName = $this->ask('What is your name? Can be your name, or a company/org name.'); 33 | $developerEmail = $this->ask('What is your email? Can be a specific email like jon@gmail.com or developers@company.com.'); 34 | $developerUrl = $this->ask('What is the url for the project? Can be a Github/Gitlab link, site url, company url etc.'); 35 | $appVersion = $this->ask('What version of the application are you on?', '0.1.0'); 36 | $appDescription = $this->ask('What is a description of the app?'); 37 | $directoryName = $this->ask('Where do you want the spec file to live? The default is set in the config/openapi.php'); 38 | $this->output-> writeLn("\r\n"); 39 | 40 | $this->info( 41 | 'The contract is generated with empty fields for you to populate as you develop. 42 | If you have questions about the spec here is the official guide: 43 | https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md' 44 | ); 45 | 46 | $this->output-> writeLn("\r\n"); 47 | 48 | $data = [ 49 | 'openapi' => $oasVersion, 50 | 'info' => [ 51 | 'title' => (string) $appName, 52 | 'version' => $appVersion, 53 | 'description' => $appDescription, 54 | 'contact' => [ 55 | 'name' => $developerName, 56 | 'email' => $developerEmail, 57 | 'url' => $developerUrl 58 | ] 59 | ], 60 | 'servers' => [ 61 | [ 62 | 'url' => config('openapi.url'), 63 | 'description' => config('openapi.url_description') 64 | ], 65 | ], 66 | 'tags' => null, 67 | 'paths' => null, 68 | 'components' => [ 69 | 'schemas' => null, 70 | 'responses' => null, 71 | 'headers' => null, 72 | 'parameters' => null, 73 | 'links' => null, 74 | 'examples' => null 75 | ] 76 | ]; 77 | 78 | $contract = Yaml::dump($data); 79 | 80 | if ($directoryName) { 81 | $this->createContractWithDirectoryName($directoryName, $contract); 82 | } else { 83 | $this->createContractInDefaultDirectory($contract); 84 | } 85 | $this->info('It\'s dangerous to go alone. Take this: https://stoplight.io/studio/'); 86 | 87 | $installSpectral = $this->confirm('Do you want to install Stoplight Spectral so you can lint your OpenAPI contract as you build it?'); 88 | 89 | if ($installSpectral) { 90 | $this->info('Installing Spectral'); 91 | exec('npm install -g @stoplight/spectral'); 92 | $this->output-> writeLn("\r\n"); 93 | $this->info('To call Spectral, run this command:'); 94 | $this->line(sprintf('spectral lint %s/openapi.yml', config('openapi.contract_directory'))); 95 | } 96 | 97 | $this->info('All finished. Happy coding! If you want to see more tools that integrate into the OpenAPI toolset, please visit https://openapi.tools'); 98 | } 99 | 100 | private function createContractWithDirectoryName(string $directoryName, string $contract) 101 | { 102 | $this->info(sprintf('Creating directory at: %s', sprintf('./%s', $directoryName))); 103 | 104 | mkdir(sprintf('./%s', $directoryName)); 105 | 106 | file_put_contents(sprintf('./%s/openapi.yml', $directoryName), $contract); 107 | $this->output-> writeLn("\r\n"); 108 | $this->info(sprintf('Contract created at: ./%s/openapi.yml', $directoryName)); 109 | } 110 | 111 | private function createContractInDefaultDirectory(string $contract) 112 | { 113 | $this->info(sprintf('Creating directory at: %s', sprintf('./%s', config('openapi.contract_directory')))); 114 | 115 | mkdir(sprintf('./%s', config('openapi.contract_directory'))); 116 | 117 | file_put_contents(sprintf('./%s/openapi.yml', config('openapi.contract_directory')), $contract); 118 | $this->output-> writeLn("\r\n"); 119 | $this->info(sprintf('Contract created at: ./%s/openapi.yml', config('openapi.contract_directory'))); 120 | } 121 | } --------------------------------------------------------------------------------