Browse Source

Merge branch 'master' into 4.next

Mark Story 5 years ago
parent
commit
aaf0a4d964

+ 5 - 4
.github/workflows/ci.yml

@@ -89,12 +89,12 @@ jobs:
         sudo locale-gen da_DK.UTF-8
         sudo locale-gen de_DE.UTF-8
 
-    - name: composer install
+    - name: Composer install
       run: |
         if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
           composer update --prefer-lowest --prefer-stable
         else
-          composer update
+          composer install
         fi
 
     - name: Setup problem matchers for PHPUnit
@@ -177,7 +177,7 @@ jobs:
         path: ${{ steps.composer-cache.outputs.dir }}
         key: ${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }}
 
-    - name: composer install
+    - name: Composer install
       run: composer install
 
     - name: Run PHPUnit
@@ -198,6 +198,7 @@ jobs:
       with:
         php-version: '7.3'
         extensions: mbstring, intl, apcu, memcached, redis
+        tools: cs2pr
         coverage: none
 
     - name: Get composer cache directory
@@ -218,7 +219,7 @@ jobs:
       run: composer stan-setup
 
     - name: Run PHP CodeSniffer
-      run: vendor/bin/phpcs --report=checkstyle src/ tests/
+      run: vendor/bin/phpcs --report=checkstyle src/ tests/ | cs2pr
 
     - name: Run psalm
       if: success() || failure()

+ 1 - 1
src/Database/Log/LoggingStatement.php

@@ -73,6 +73,7 @@ class LoggingStatement extends StatementDecorator
 
         try {
             $result = parent::execute($params);
+            $this->loggedQuery->took = (int)round((microtime(true) - $this->startTime) * 1000, 0);
         } catch (Exception $e) {
             /** @psalm-suppress UndefinedPropertyAssignment */
             $e->queryString = $this->queryString;
@@ -143,7 +144,6 @@ class LoggingStatement extends StatementDecorator
             return;
         }
 
-        $this->loggedQuery->took = (int)round((microtime(true) - $this->startTime) * 1000, 0);
         $this->loggedQuery->query = $this->queryString;
         $this->getLogger()->debug((string)$this->loggedQuery, ['query' => $this->loggedQuery]);
 

+ 15 - 3
src/Routing/RouteBuilder.php

@@ -868,8 +868,14 @@ class RouteBuilder
      * Routes connected in the scoped collection will have the correct path segment
      * prepended, and have a matching plugin routing key set.
      *
+     * ### Options
+     *
+     * - `path` The path prefix to use. Defaults to `Inflector::dasherize($name)`.
+     * - `_namePrefix` Set a prefix used for named routes. The prefix is prepended to the
+     *   name of any route created in a scope callback.
+     *
      * @param string $name The plugin name to build routes for
-     * @param array|callable $options Either the options to use, or a callback
+     * @param array|callable $options Either the options to use, or a callback to build routes.
      * @param callable|null $callback The callback to invoke that builds the plugin routes
      *   Only required when $options is defined.
      * @return $this
@@ -881,9 +887,10 @@ class RouteBuilder
             $options = [];
         }
 
-        $params = ['plugin' => $name] + $this->_params;
         $path = $options['path'] ?? '/' . Inflector::dasherize($name);
-        $this->scope($path, $params, $callback);
+        unset($options['path']);
+        $options = ['plugin' => $name] + $options;
+        $this->scope($path, $options, $callback);
 
         return $this;
     }
@@ -895,6 +902,11 @@ class RouteBuilder
      * added to. This means that both the current path and parameters will be appended
      * to the supplied parameters.
      *
+     * ### Special Keys in $params
+     *
+     * - `_namePrefix` Set a prefix used for named routes. The prefix is prepended to the
+     *   name of any route created in a scope callback.
+     *
      * @param string $path The path to create a scope for.
      * @param array|callable $params Either the parameters to add to routes, or a callback.
      * @param callable|null $callback The callback to invoke that builds the plugin routes.

+ 21 - 2
tests/TestCase/Routing/RouteBuilderTest.php

@@ -531,7 +531,7 @@ class RouteBuilderTest extends TestCase
      *
      * @return void
      */
-    public function testNestedPlugin()
+    public function testPlugin()
     {
         $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
         $res = $routes->plugin('Contacts', function (RouteBuilder $r) {
@@ -553,7 +553,7 @@ class RouteBuilderTest extends TestCase
      *
      * @return void
      */
-    public function testNestedPluginPathOption()
+    public function testPluginPathOption()
     {
         $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
         $routes->plugin('Contacts', ['path' => '/people'], function (RouteBuilder $r) {
@@ -563,6 +563,25 @@ class RouteBuilderTest extends TestCase
     }
 
     /**
+     * Test creating sub-scopes with plugin() + namePrefix option
+     *
+     * @return void
+     */
+    public function testPluginNamePrefix()
+    {
+        $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
+        $routes->plugin('Contacts', ['_namePrefix' => 'contacts.'], function (RouteBuilder $r) {
+            $this->assertEquals('contacts.', $r->namePrefix());
+        });
+
+        $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
+        $routes->namePrefix('default.');
+        $routes->plugin('Blog', ['_namePrefix' => 'blog.'], function (RouteBuilder $r) {
+            $this->assertEquals('default.blog.', $r->namePrefix(), 'Should combine nameprefix');
+        });
+    }
+
+    /**
      * Test connecting resources.
      *
      * @return void