فهرست منبع

Using uniqid for tmp file name generation appears to be causing conflicts when running processes in parallel. Have replaced for random string generation seeded by mt_rand - https://github.com/jonnnnyw/php-phantomjs/issues/101

Jonny Wenmoth 9 سال پیش
والد
کامیت
ee6cecba62

+ 2 - 1
src/JonnyW/PhantomJs/Cache/FileCache.php

@@ -8,6 +8,7 @@
  */
 namespace JonnyW\PhantomJs\Cache;
 
+use JonnyW\PhantomJs\StringUtils;
 use JonnyW\PhantomJs\Exception\NotWritableException;
 use JonnyW\PhantomJs\Exception\NotExistsException;
 
@@ -169,7 +170,7 @@ class FileCache implements CacheInterface
     protected function getFileName($id)
     {
         if (is_dir($id)) {
-            return sprintf('%1$s/%2$s.%3$s', rtrim($id, DIRECTORY_SEPARATOR), uniqid(), $this->extension);
+            return sprintf('%1$s/%2$s.%3$s', rtrim($id, DIRECTORY_SEPARATOR), StringUtils::random(20), $this->extension);
         }
 
         $dirName = dirname($id);

+ 1 - 1
src/JonnyW/PhantomJs/Http/AbstractRequest.php

@@ -404,7 +404,7 @@ abstract class AbstractRequest
      * Get body styles
      *
      * @access public
-     * @param string $format (default: 'default')
+     * @param  string $format (default: 'default')
      * @return array
      */
     public function getBodyStyles($format = 'default')

+ 2 - 1
src/JonnyW/PhantomJs/Procedure/Procedure.php

@@ -14,6 +14,7 @@ use JonnyW\PhantomJs\Parser\ParserInterface;
 use JonnyW\PhantomJs\Template\TemplateRendererInterface;
 use JonnyW\PhantomJs\Exception\NotWritableException;
 use JonnyW\PhantomJs\Exception\ProcedureFailedException;
+use JonnyW\PhantomJs\StringUtils;
 
 /**
  * PHP PhantomJs
@@ -184,7 +185,7 @@ class Procedure implements ProcedureInterface
      */
     protected function write($compiled)
     {
-        return $this->cacheHandler->save(uniqid(), $compiled);
+        return $this->cacheHandler->save(StringUtils::random(20), $compiled);
     }
 
     /**

+ 30 - 0
src/JonnyW/PhantomJs/StringUtils.php

@@ -0,0 +1,30 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+namespace JonnyW\PhantomJs;
+
+/**
+ * PHP PhantomJs
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+final class StringUtils
+{
+    /**
+     * Generate random string
+     *
+     * @static
+     * @access public
+     * @param  int    $length (default: 20)
+     * @return string
+     */
+    public static function random($length = 20)
+    {
+        return substr(md5(mt_rand()), 0, $length);
+    }
+}

+ 1 - 1
src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php

@@ -751,7 +751,7 @@ EOF;
     }
 
     /**
-     * Test test can set page 
+     * Test test can set page
      * background color
      *
      * @access public

+ 52 - 0
src/JonnyW/PhantomJs/Tests/Unit/StringUtilsTest.php

@@ -0,0 +1,52 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+namespace JonnyW\PhantomJs\Tests\Unit;
+
+use JonnyW\PhantomJs\StringUtils;
+
+/**
+ * PHP PhantomJs
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class StringUtilsTest extends \PHPUnit_Framework_TestCase
+{
+
+/** +++++++++++++++++++++++++++++++++++ **/
+/** ++++++++++++++ TESTS ++++++++++++++ **/
+/** +++++++++++++++++++++++++++++++++++ **/
+
+    /**
+     * Test can generate random string for
+     * specific length
+     *
+     * @access public
+     * @return void
+     */
+    public function testCanGenerateRandomStringForSpecificLength()
+    {
+        $string = StringUtils::random(14);
+
+        $this->assertEquals(14, strlen($string));
+    }
+
+    /**
+     * Test random string is random
+     *
+     * @access public
+     * @return void
+     */
+    public function testRandomStringIsRandom()
+    {
+        $string1 = StringUtils::random(14);
+        $string2 = StringUtils::random(14);
+
+        $this->assertNotEquals($string1, $string2);
+    }
+}