createSingleFile.php 15 KB

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