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