createSingleFile.php 16 KB

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