createSingleFile.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #!/usr/bin/env php
  2. <?php
  3. // This script can be used to automatically glue all the .php files of Predis
  4. // into a single monolithic script file that can be used without an autoloader,
  5. // just like the other previous versions of the library.
  6. //
  7. // Much of its complexity is due to the fact that we cannot simply join PHP
  8. // files, but namespaces and classes definitions must follow a precise order
  9. // when dealing with subclassing and inheritance.
  10. //
  11. // The current implementation is pretty naïve, but it should do for now.
  12. //
  13. /* -------------------------------------------------------------------------- */
  14. class CommandLine {
  15. public static function getOptions() {
  16. $parameters = array(
  17. 's:' => 'source:',
  18. 'o:' => 'output:',
  19. 'e:' => 'exclude:',
  20. 'E:' => 'exclude-classes:',
  21. );
  22. $getops = getopt(implode(array_keys($parameters)), $parameters);
  23. $options = array(
  24. 'source' => __DIR__ . "/../lib/",
  25. 'output' => PredisFile::NS_ROOT . '.php',
  26. 'exclude' => array(),
  27. );
  28. foreach ($getops as $option => $value) {
  29. switch ($option) {
  30. case 's':
  31. case 'source':
  32. $options['source'] = $value;
  33. break;
  34. case 'o':
  35. case 'output':
  36. $options['output'] = $value;
  37. break;
  38. case 'E':
  39. case 'exclude-classes':
  40. $options['exclude'] = @file($value, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: $value;
  41. break;
  42. case 'e':
  43. case 'exclude':
  44. $options['exclude'] = is_array($value) ? $value : array($value);
  45. break;
  46. }
  47. }
  48. return $options;
  49. }
  50. }
  51. class PredisFile {
  52. const NS_ROOT = 'Predis';
  53. private $_namespaces;
  54. public function __construct() {
  55. $this->_namespaces = array();
  56. }
  57. public static function from($libraryPath, Array $exclude = array()) {
  58. $nsroot = self::NS_ROOT;
  59. $predisFile = new PredisFile();
  60. $libIterator = new RecursiveDirectoryIterator("$libraryPath$nsroot");
  61. foreach (new RecursiveIteratorIterator($libIterator) as $classFile) {
  62. if (!$classFile->isFile()) {
  63. continue;
  64. }
  65. $namespace = strtr(str_replace($libraryPath, '', $classFile->getPath()), '/', '\\');
  66. if (in_array(sprintf('%s\\%s', $namespace, $classFile->getBasename('.php')), $exclude)) {
  67. continue;
  68. }
  69. $phpNamespace = $predisFile->getNamespace($namespace);
  70. if ($phpNamespace === false) {
  71. $phpNamespace = new PhpNamespace($namespace);
  72. $predisFile->addNamespace($phpNamespace);
  73. }
  74. $phpClass = new PhpClass($phpNamespace, $classFile);
  75. }
  76. return $predisFile;
  77. }
  78. public function addNamespace(PhpNamespace $namespace) {
  79. if (isset($this->_namespaces[(string)$namespace])) {
  80. throw new InvalidArgumentException("Duplicated namespace");
  81. }
  82. $this->_namespaces[(string)$namespace] = $namespace;
  83. }
  84. public function getNamespaces() {
  85. return $this->_namespaces;
  86. }
  87. public function getNamespace($namespace) {
  88. if (!isset($this->_namespaces[$namespace])) {
  89. return false;
  90. }
  91. return $this->_namespaces[$namespace];
  92. }
  93. public function getClassByFQN($classFqn) {
  94. if (($nsLastPos = strrpos($classFqn, '\\')) !== false) {
  95. $namespace = $this->getNamespace(substr($classFqn, 0, $nsLastPos));
  96. if ($namespace === false) {
  97. return null;
  98. }
  99. $className = substr($classFqn, $nsLastPos + 1);
  100. return $namespace->getClass($className);
  101. }
  102. return null;
  103. }
  104. private function calculateDependencyScores(&$classes, $fqn) {
  105. if (!isset($classes[$fqn])) {
  106. $classes[$fqn] = 0;
  107. }
  108. $classes[$fqn] += 1;
  109. if (($phpClass = $this->getClassByFQN($fqn)) === null) {
  110. throw new RuntimeException("Cannot found the class $fqn which is required by other subclasses. Are you missing a file?");
  111. }
  112. foreach ($phpClass->getDependencies() as $fqn) {
  113. $this->calculateDependencyScores($classes, $fqn);
  114. }
  115. }
  116. private function getDependencyScores() {
  117. $classes = array();
  118. foreach ($this->getNamespaces() as $phpNamespace) {
  119. foreach ($phpNamespace->getClasses() as $phpClass) {
  120. $this->calculateDependencyScores($classes, $phpClass->getFQN());
  121. }
  122. }
  123. return $classes;
  124. }
  125. private function getOrderedNamespaces($dependencyScores) {
  126. $namespaces = array_fill_keys(array_unique(
  127. array_map(
  128. function($fqn) { return PhpNamespace::extractName($fqn); },
  129. array_keys($dependencyScores)
  130. )
  131. ), 0);
  132. foreach ($dependencyScores as $classFqn => $score) {
  133. $namespaces[PhpNamespace::extractName($classFqn)] += $score;
  134. }
  135. arsort($namespaces);
  136. return array_keys($namespaces);
  137. }
  138. private function getOrderedClasses(PhpNamespace $phpNamespace, $classes) {
  139. $nsClassesFQNs = array_map(
  140. function($cl) { return $cl->getFQN(); },
  141. $phpNamespace->getClasses()
  142. );
  143. $nsOrderedClasses = array();
  144. foreach ($nsClassesFQNs as $nsClassFQN) {
  145. $nsOrderedClasses[$nsClassFQN] = $classes[$nsClassFQN];
  146. }
  147. arsort($nsOrderedClasses);
  148. return array_keys($nsOrderedClasses);
  149. }
  150. public function getPhpCode() {
  151. $buffer = array("<?php\n\n");
  152. $classes = $this->getDependencyScores();
  153. $namespaces = $this->getOrderedNamespaces($classes);
  154. foreach ($namespaces as $namespace) {
  155. $phpNamespace = $this->getNamespace($namespace);
  156. // generate namespace directive
  157. $buffer[] = $phpNamespace->getPhpCode();
  158. $buffer[] = "\n";
  159. // generate use directives
  160. $useDirectives = $phpNamespace->getUseDirectives();
  161. if (count($useDirectives) > 0) {
  162. $buffer[] = $useDirectives->getPhpCode();
  163. $buffer[] = "\n";
  164. }
  165. // generate classes bodies
  166. $nsClasses = $this->getOrderedClasses($phpNamespace, $classes);
  167. foreach ($nsClasses as $classFQN) {
  168. $buffer[] = $this->getClassByFQN($classFQN)->getPhpCode();
  169. $buffer[] = "\n\n";
  170. }
  171. $buffer[] = "/* " . str_repeat("-", 75) . " */";
  172. $buffer[] = "\n\n";
  173. }
  174. return implode($buffer);
  175. }
  176. public function saveTo($outputFile) {
  177. // TODO: add more sanity checks
  178. if ($outputFile === null || $outputFile === '') {
  179. throw new InvalidArgumentException('You must specify a valid output file');
  180. }
  181. file_put_contents($outputFile, $this->getPhpCode());
  182. }
  183. }
  184. class PhpNamespace implements IteratorAggregate {
  185. private $_namespace, $_classes;
  186. public function __construct($namespace) {
  187. $this->_namespace = $namespace;
  188. $this->_classes = array();
  189. $this->_useDirectives = new PhpUseDirectives($this);
  190. }
  191. public static function extractName($fqn) {
  192. $nsSepLast = strrpos($fqn, '\\');
  193. if ($nsSepLast === false) {
  194. return $fqn;
  195. }
  196. $ns = substr($fqn, 0, $nsSepLast);
  197. return $ns !== '' ? $ns : null;
  198. }
  199. public function addClass(PhpClass $class) {
  200. $this->_classes[$class->getName()] = $class;
  201. }
  202. public function getClass($className) {
  203. if (isset($this->_classes[$className])) {
  204. return $this->_classes[$className];
  205. }
  206. }
  207. public function getClasses() {
  208. return array_values($this->_classes);
  209. }
  210. public function getIterator() {
  211. return new \ArrayIterator($this->getClasses());
  212. }
  213. public function getUseDirectives() {
  214. return $this->_useDirectives;
  215. }
  216. public function getPhpCode() {
  217. return "namespace $this->_namespace;\n";
  218. }
  219. public function __toString() {
  220. return $this->_namespace;
  221. }
  222. }
  223. class PhpUseDirectives implements Countable, IteratorAggregate {
  224. private $_use, $_aliases, $_namespace;
  225. public function __construct(PhpNamespace $namespace) {
  226. $this->_use = array();
  227. $this->_aliases = array();
  228. $this->_namespace = $namespace;
  229. }
  230. public function add($use, $as = null) {
  231. if (!in_array($use, $this->_use)) {
  232. $this->_use[] = $use;
  233. $this->_aliases[$as ?: PhpClass::extractName($use)] = $use;
  234. }
  235. }
  236. public function getList() {
  237. return $this->_use;
  238. }
  239. public function getIterator() {
  240. return new \ArrayIterator($this->getList());
  241. }
  242. public function getPhpCode() {
  243. $reducer = function($str, $use) { return $str .= "use $use;\n"; };
  244. return array_reduce($this->getList(), $reducer, '');
  245. }
  246. public function getNamespace() {
  247. return $this->_namespace;
  248. }
  249. public function getFQN($className) {
  250. if (($nsSepFirst = strpos($className, '\\')) === false) {
  251. if (isset($this->_aliases[$className])) {
  252. return $this->_aliases[$className];
  253. }
  254. return (string)$this->getNamespace() . "\\$className";
  255. }
  256. if ($nsSepFirst != 0) {
  257. throw new InvalidArgumentException("Partially qualified names are not supported");
  258. }
  259. return $className;
  260. }
  261. public function count() {
  262. return count($this->_use);
  263. }
  264. }
  265. class PhpClass {
  266. private $_namespace, $_file, $_body, $_implements, $_extends, $_name;
  267. public function __construct(PhpNamespace $namespace, SplFileInfo $classFile) {
  268. $this->_namespace = $namespace;
  269. $this->_file = $classFile;
  270. $this->_implements = array();
  271. $this->_extends = array();
  272. $this->extractData();
  273. $namespace->addClass($this);
  274. }
  275. public static function extractName($fqn) {
  276. $nsSepLast = strrpos($fqn, '\\');
  277. if ($nsSepLast === false) {
  278. return $fqn;
  279. }
  280. return substr($fqn, $nsSepLast + 1);
  281. }
  282. private function extractData() {
  283. $useDirectives = $this->getNamespace()->getUseDirectives();
  284. $useExtractor = function($m) use($useDirectives) {
  285. $useDirectives->add(($namespacedPath = $m[1]));
  286. };
  287. $classBuffer = stream_get_contents(fopen($this->getFile()->getPathname(), 'r'));
  288. $classBuffer = preg_replace('/<\?php\s?\\n\s?/', '', $classBuffer);
  289. $classBuffer = preg_replace('/\s?\?>\n?/ms', '', $classBuffer);
  290. $classBuffer = preg_replace('/namespace\s+[\w\d_\\\]+;\s?/', '', $classBuffer);
  291. $classBuffer = preg_replace_callback('/use\s+([\w\d_\\\]+)(\s+as\s+.*)?;\s?\n?/', $useExtractor, $classBuffer);
  292. $this->_body = trim($classBuffer);
  293. $this->extractHierarchy();
  294. }
  295. private function extractHierarchy() {
  296. $implements = array();
  297. $extends = array();
  298. $extractor = function($iterator, $callback) {
  299. $className = '';
  300. $iterator->seek($iterator->key() + 1);
  301. while ($iterator->valid()) {
  302. $token = $iterator->current();
  303. if (is_string($token)) {
  304. if (preg_match('/\s?,\s?/', $token)) {
  305. $callback(trim($className));
  306. $className = '';
  307. }
  308. else if ($token == '{') {
  309. $callback(trim($className));
  310. return;
  311. }
  312. }
  313. switch ($token[0]) {
  314. case T_NS_SEPARATOR:
  315. $className .= '\\';
  316. break;
  317. case T_STRING:
  318. $className .= $token[1];
  319. break;
  320. case T_IMPLEMENTS:
  321. case T_EXTENDS:
  322. $callback(trim($className));
  323. $iterator->seek($iterator->key() - 1);
  324. return;
  325. }
  326. $iterator->next();
  327. }
  328. };
  329. $tokens = token_get_all("<?php\n" . trim($this->getPhpCode()));
  330. $iterator = new ArrayIterator($tokens);
  331. while ($iterator->valid()) {
  332. $token = $iterator->current();
  333. if (is_string($token)) {
  334. $iterator->next();
  335. continue;
  336. }
  337. switch ($token[0]) {
  338. case T_CLASS:
  339. case T_INTERFACE:
  340. $iterator->seek($iterator->key() + 2);
  341. $tk = $iterator->current();
  342. $this->_name = $tk[1];
  343. break;
  344. case T_IMPLEMENTS:
  345. $extractor($iterator, function($fqn) use (&$implements) {
  346. $implements[] = $fqn;
  347. });
  348. break;
  349. case T_EXTENDS:
  350. $extractor($iterator, function($fqn) use (&$extends) {
  351. $extends[] = $fqn;
  352. });
  353. break;
  354. }
  355. $iterator->next();
  356. }
  357. $this->_implements = $this->guessFQN($implements);
  358. $this->_extends = $this->guessFQN($extends);
  359. }
  360. public function guessFQN($classes) {
  361. $useDirectives = $this->getNamespace()->getUseDirectives();
  362. return array_map(array($useDirectives, 'getFQN'), $classes);
  363. }
  364. public function getImplementedInterfaces($all = false) {
  365. if ($all) {
  366. return $this->_implements;
  367. }
  368. else {
  369. return array_filter(
  370. $this->_implements,
  371. function ($cn) { return strpos($cn, 'Predis\\') === 0; }
  372. );
  373. }
  374. }
  375. public function getExtendedClasses($all = false) {
  376. if ($all) {
  377. return $this->_extemds;
  378. }
  379. else {
  380. return array_filter(
  381. $this->_extends,
  382. function ($cn) { return strpos($cn, 'Predis\\') === 0; }
  383. );
  384. }
  385. }
  386. public function getDependencies($all = false) {
  387. return array_merge(
  388. $this->getImplementedInterfaces($all),
  389. $this->getExtendedClasses($all)
  390. );
  391. }
  392. public function getNamespace() {
  393. return $this->_namespace;
  394. }
  395. public function getFile() {
  396. return $this->_file;
  397. }
  398. public function getName() {
  399. return $this->_name;
  400. }
  401. public function getFQN() {
  402. return (string)$this->getNamespace() . '\\' . $this->_name;
  403. }
  404. public function getPhpCode() {
  405. return $this->_body;
  406. }
  407. public function __toString() {
  408. return "class " . $this->getName() . '{ ... }';
  409. }
  410. }
  411. /* -------------------------------------------------------------------------- */
  412. $options = CommandLine::getOptions();
  413. $predisFile = PredisFile::from($options['source'], $options['exclude']);
  414. $predisFile->saveTo($options['output']);