Skip to content

Commit 2b08b8e

Browse files
committed
Replace the readme updater script with a callable
1 parent af3746b commit 2b08b8e

File tree

3 files changed

+100
-47
lines changed

3 files changed

+100
-47
lines changed

composer.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@
99
"homepage": "http://aws.amazon.com"
1010
}
1111
],
12-
"require-dev": {
13-
"phpunit/phpunit": "~4.7",
14-
"symfony/framework-bundle": "~2.3",
15-
"symfony/finder": "~2.3",
16-
"symfony/yaml": "~2.3",
17-
"psy/psysh": "~0.4.4"
18-
},
1912
"require": {
2013
"aws/aws-sdk-php": "^3.0.5",
2114
"symfony/config": "~2.3",
2215
"symfony/dependency-injection": "~2.3",
2316
"symfony/http-kernel": "~2.3",
2417
"doctrine/inflector": "~1.0"
2518
},
19+
"require-dev": {
20+
"phpunit/phpunit": "~4.7",
21+
"symfony/framework-bundle": "~2.3",
22+
"symfony/finder": "~2.3",
23+
"symfony/yaml": "~2.3"
24+
},
2625
"autoload": {
2726
"psr-4": {
2827
"Aws\\Symfony\\": "src/"
@@ -32,5 +31,10 @@
3231
"psr-4": {
3332
"Aws\\Symfony\\": "tests/"
3433
}
34+
},
35+
"scripts": {
36+
"post-autoload-dump": [
37+
"Aws\\Symfony\\ReadMeUpdater::updateReadMe"
38+
]
3539
}
3640
}

generateAvailableServices.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/ReadMeUpdater.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Aws\Symfony;
4+
5+
use Symfony\Component\DependencyInjection\Container;
6+
7+
class ReadMeUpdater
8+
{
9+
const SERVICES_TABLE_START = '<!-- BEGIN SERVICE TABLE -->';
10+
const SERVICES_TABLE_END = '<!-- END SERVICE TABLE -->';
11+
const SERVICE_CLASS_DELIMITER = '@CLASS@';
12+
const DOCS_URL_TEMPLATE = 'http://docs.aws.amazon.com/aws-sdk-php/v3/api/class-'
13+
. self::SERVICE_CLASS_DELIMITER . '.html';
14+
15+
public static function updateReadMe()
16+
{
17+
require __DIR__ . '/../vendor/autoload.php';
18+
(new static)->doUpdateReadMe();
19+
}
20+
21+
22+
protected function doUpdateReadMe()
23+
{
24+
$readMeParts = $this->getReadMeWithoutServicesTable();
25+
$servicesTable = self::SERVICES_TABLE_START . "\n"
26+
. $this->getServicesTable()
27+
. self::SERVICES_TABLE_END;
28+
29+
$updatedReadMe = implode($servicesTable, $readMeParts);
30+
file_put_contents($this->getReadMePath(), $updatedReadMe);
31+
}
32+
33+
protected function getReadMeWithoutServicesTable()
34+
{
35+
$readMe = file_get_contents($this->getReadMePath());
36+
$tablePattern = '/' . preg_quote(self::SERVICES_TABLE_START)
37+
. '.*' . preg_quote(self::SERVICES_TABLE_END) . '/s';
38+
39+
return preg_split($tablePattern, $readMe, 2);
40+
}
41+
42+
protected function getReadMePath()
43+
{
44+
return dirname(__DIR__) . '/README.md';
45+
}
46+
47+
protected function getServicesTable()
48+
{
49+
$table = "Service | Instance Of\n--- | ---\n";
50+
51+
$container = $this->getContainer();
52+
foreach ($this->getAWSServices($container) as $service) {
53+
$serviceClass = get_class($container->get($service));
54+
$apiDocLink = preg_replace(
55+
'/' . preg_quote(self::SERVICE_CLASS_DELIMITER) . '/',
56+
str_replace('\\', '.', $serviceClass),
57+
self::DOCS_URL_TEMPLATE
58+
);
59+
60+
$table .= "$service | [$serviceClass]($apiDocLink) \n";
61+
}
62+
63+
return $table;
64+
}
65+
66+
protected function getAWSServices(Container $container)
67+
{
68+
return array_filter($container->getServiceIds(), function ($service) {
69+
return strpos($service, 'aws.') === 0;
70+
});
71+
}
72+
73+
/**
74+
* @return Container
75+
*/
76+
protected function getContainer()
77+
{
78+
static $container = null;
79+
80+
if (empty($container)) {
81+
require_once __DIR__ . '/fixtures/AppKernel.php';
82+
$kernel = new \AppKernel('test', true);
83+
$kernel->boot();
84+
$container = $kernel->getContainer();
85+
}
86+
87+
return $container;
88+
}
89+
}

0 commit comments

Comments
 (0)