Skip to content

Commit

Permalink
Added a way to automagically generate the configuration part of the R…
Browse files Browse the repository at this point in the history
…EADME
  • Loading branch information
weaverryan committed Jan 16, 2016
1 parent be4abc9 commit f82e52b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 14 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,29 +164,29 @@ class FacebookController extends Controller
# app/config/config.yml
knpu_oauth2_client:
providers:
# the key "facebook_client" can be anything: it determines the service name
# will create a service: "knpu.oauth2.facebook_client"
# will create service: "knpu.oauth2.facebook_client"
# composer require league/oauth2-facebook
facebook_client:
# must be one of the valid types
# must be "facebook" - it activates that type!
type: facebook
client_id: foo
client_secret: bar
graph_api_version: 2.3
# the route that you're redirected to after
client_id: Your_Real_Client_Id
client_secret: Your_Real_Client_Secret
# a route name you'll create
redirect_route: connect_facebook_check
redirect_params: {}
graph_api_version: v2.5
# will create a service: "knpu.oauth2.github_client"
# will create service: "knpu.oauth2.github_client"
# composer require league/oauth2-github
github_client:
# must be one of the valid types
# must be "github" - it activates that type!
type: github
client_id: foo
client_secret: bar
client_id: Your_Real_Client_Id
client_secret: Your_Real_Client_Secret
# a route name you'll create
redirect_route: connect_github_check
redirect_params: {}
# todo - add more
# http://oauth2-client.thephpleague.com/providers/thirdparty/
```

## Contributing
Expand Down
97 changes: 97 additions & 0 deletions bin/update_readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env php
<?php

/*
* Run this script to automatically update the Configuratio section
* of the README.md file.
*/

require __DIR__.'/../vendor/autoload.php';

use KnpU\OAuth2ClientBundle\DependencyInjection\KnpUOAuth2ClientExtension;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\NodeInterface;

$sectionTemplate = <<<EOF
# will create service: "knpu.oauth2.%PROVIDER_NAME%_client"
# composer require %PACKAGE_NAME%
%PROVIDER_NAME%_client:
# must be "%PROVIDER_NAME%" - it activates that type!
type: %PROVIDER_NAME%
client_id: Your_Real_Client_Id
client_secret: Your_Real_Client_Secret
# a route name you'll create
redirect_route: connect_%PROVIDER_NAME%_check
redirect_params: {}
%ADDITIONAL_CONFIG%
EOF;

$extension = new KnpUOAuth2ClientExtension();
$configSections = [];
foreach (KnpUOAuth2ClientExtension::getAllSupportedTypes() as $type) {
$tree = new TreeBuilder();
$configNode = $tree->root('generating_readme');
$configurator = $extension->getConfigurator($type);
$configurator->buildConfiguration($configNode->children());

/** @var ArrayNode $arrayNode */
$arrayNode = $tree->buildTree();
$customKeys = array();

foreach ($arrayNode->getChildren() as $child) {
/** @var NodeInterface $child */

if ($child instanceof ArrayNode) {
$defaultValue = $child->getDefaultValue()
// *should* ? come out looking like valid-ish YAML
? json_encode($child->getDefaultValue())
: '{}';
} else {
$defaultValue = $child->getDefaultValue()
? $child->getDefaultValue()
: "''";
}

$customKeys[] = sprintf('%s: %s', $child->getName(), $defaultValue);
}

$newSection = str_replace('%PROVIDER_NAME%', $type, $sectionTemplate);
$newSection = str_replace('%PACKAGE_NAME%', $configurator->getPackagistName(), $newSection);
$newSection = str_replace(
'%ADDITIONAL_CONFIG%',
implode("\n ", $customKeys),
$newSection
);
$configSections[] = $newSection;
}

$configurationText = <<<EOF
## Configuration
```yml
# app/config/config.yml
knpu_oauth2_client:
providers:
%PROVIDERS_ENTRIES%
```
## Contributing
EOF;

$finalConfigurationText = str_replace(
'%PROVIDERS_ENTRIES%',
implode("\n\n", $configSections),
$configurationText
);

$currentContents = file_get_contents(__DIR__.'/../README.md');
$startPos = strpos($currentContents, '## Configuration');
$endPos = strpos($currentContents, '## Contributing');

$newReadmeContents = substr($currentContents, 0, $startPos);
$newReadmeContents .= $finalConfigurationText;
$newReadmeContents .= substr($currentContents, $endPos+15);

file_put_contents(__DIR__.'/../README.md', $newReadmeContents);
echo "\n\n README.md Updated!\n\n";

1 comment on commit f82e52b

@kbond
Copy link

@kbond kbond commented on f82e52b Jan 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!!

Please sign in to comment.