create-single-file 18 KB

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