Переглянути джерело

Merge branch 'v0.9/fix-bin-scripts'

Daniele Alessandri 11 роки тому
батько
коміт
b7a7b6838b
5 змінених файлів з 66 додано та 9 видалено
  1. 2 2
      README.md
  2. 0 0
      bin/create-command-test
  3. 0 0
      bin/create-phar
  4. 63 6
      bin/create-single-file
  5. 1 1
      tests/README.md

+ 2 - 2
README.md

@@ -56,10 +56,10 @@ Predis\Autoloader::register();
 ```
 
 It is also possible to create a [phar](http://www.php.net/manual/en/intro.phar.php) archive from the
-repository just by launching `bin/create-phar.php`. The generated phar ships with a stub defining an
+repository just by launching `bin/create-phar`. The generated phar contains a stub that defines an
 autoloader function for Predis, so you just need to require the phar to start using the library.
 Alternatively it is possible to generate one single PHP file holding every class like older versions
-of Predis by launching `bin/create-single-file.php`, but this practice __is not__ encouraged.
+of Predis by launching `bin/create-single-file`, but this practice __is not__ encouraged.
 
 
 ### Connecting to Redis ###

+ 0 - 0
bin/generate-command-test.php → bin/create-command-test


+ 0 - 0
bin/create-phar.php → bin/create-phar


+ 63 - 6
bin/create-single-file.php → bin/create-single-file

@@ -321,13 +321,15 @@ class PhpUseDirectives implements Countable, IteratorAggregate
 {
     private $use;
     private $aliases;
+    private $reverseAliases;
     private $namespace;
 
     public function __construct(PhpNamespace $namespace)
     {
+        $this->namespace = $namespace;
         $this->use = array();
         $this->aliases = array();
-        $this->namespace = $namespace;
+        $this->reverseAliases = array();
     }
 
     public function add($use, $as = null)
@@ -336,8 +338,35 @@ class PhpUseDirectives implements Countable, IteratorAggregate
             return;
         }
 
+        $rename = null;
         $this->use[] = $use;
-        $this->aliases[$as ?: PhpClass::extractName($use)] = $use;
+        $aliasedClassName = $as ?: PhpClass::extractName($use);
+
+        if (isset($this->aliases[$aliasedClassName])) {
+            $parentNs = $this->getParentNamespace();
+
+            if ($parentNs && false !== $pos = strrpos($parentNs, '\\')) {
+                $parentNs = substr($parentNs, $pos);
+            }
+
+            $newAlias = "{$parentNs}_{$aliasedClassName}";
+            $rename = (object) array(
+                'namespace' => $this->namespace,
+                'from' => $aliasedClassName,
+                'to' => $newAlias,
+            );
+
+            $this->aliases[$newAlias] = $use;
+            $as = $newAlias;
+        } else {
+            $this->aliases[$aliasedClassName] = $use;
+        }
+
+        if ($as !== null) {
+            $this->reverseAliases[$use] = $as;
+        }
+
+        return $rename;
     }
 
     public function getList()
@@ -352,8 +381,14 @@ class PhpUseDirectives implements Countable, IteratorAggregate
 
     public function getPhpCode()
     {
-        $reducer = function ($str, $use) {
-            return $str .= "use $use;\n";
+        $reverseAliases = $this->reverseAliases;
+
+        $reducer = function ($str, $use) use ($reverseAliases) {
+            if (isset($reverseAliases[$use])) {
+                return $str .= "use $use as {$reverseAliases[$use]};\n";
+            } else {
+                return $str .= "use $use;\n";
+            }
         };
 
         return array_reduce($this->getList(), $reducer, '');
@@ -364,6 +399,15 @@ class PhpUseDirectives implements Countable, IteratorAggregate
         return $this->namespace;
     }
 
+    public function getParentNamespace()
+    {
+        if (false !== $pos = strrpos($this->namespace, '\\')) {
+            return substr($this->namespace, 0, $pos);
+        }
+
+        return '';
+    }
+
     public function getFQN($className)
     {
         if (($nsSepFirst = strpos($className, '\\')) === false) {
@@ -430,10 +474,19 @@ LICENSE;
 
     private function extractData()
     {
+        $renames = array();
         $useDirectives = $this->getNamespace()->getUseDirectives();
 
-        $useExtractor = function ($m) use ($useDirectives) {
-            $useDirectives->add(($namespacedPath = $m[1]));
+        $useExtractor = function ($m) use ($useDirectives, &$renames) {
+            array_shift($m);
+
+            if (isset($m[1])) {
+                $m[1] = str_replace(" as ", '', $m[1]);
+            }
+
+            if ($rename = call_user_func_array(array($useDirectives, 'add'), $m)) {
+                $renames[] = $rename;
+            }
         };
 
         $classBuffer = stream_get_contents(fopen($this->getFile()->getPathname(), 'r'));
@@ -445,6 +498,10 @@ LICENSE;
         $classBuffer = preg_replace('/namespace\s+[\w\d_\\\\]+;\s?/', '', $classBuffer);
         $classBuffer = preg_replace_callback('/use\s+([\w\d_\\\\]+)(\s+as\s+.*)?;\s?\n?/', $useExtractor, $classBuffer);
 
+        foreach ($renames as $rename) {
+            $classBuffer = str_replace($rename->from, $rename->to, $classBuffer);
+        }
+
         $this->body = trim($classBuffer);
 
         $this->extractHierarchy();

+ 1 - 1
tests/README.md

@@ -81,7 +81,7 @@ are considered valid). For example, to generate a test case for `SET` (represent
 `Predis\Command\StringSet` class):
 
 ```bash
-$ ./bin/generate-command-test.php --class=StringSet
+$ ./bin/create-command-test --class=StringSet
 ```
 Each command has its own realm (commands that operate on strings, lists, sets and such)
 and this realm is automatically inferred from the name of the specified class. The realm