Browse Source

Add static method to create context factory with defaults.

ADmad 8 years ago
parent
commit
8ee647f72d
2 changed files with 51 additions and 43 deletions
  1. 50 42
      src/View/Form/ContextFactory.php
  2. 1 1
      src/View/Helper/FormHelper.php

+ 50 - 42
src/View/Form/ContextFactory.php

@@ -38,20 +38,63 @@ class ContextFactory
      *
      * @param array $providers Array of provider callables. Each element should
      *   be of form `['type' => 'a-string', 'callable' => ..]`
-     * @param bool $addDefaults Whether default providers should be added.
      */
-    public function __construct(array $providers = [], $addDefaults = true)
+    public function __construct(array $providers = [])
     {
-        if ($addDefaults) {
-            $this->addDefaultProviders();
-        }
-
         foreach ($providers as $provider) {
-            $this->addProvider($provider['type'], $provider);
+            $this->addProvider($provider['type'], $provider['callable']);
         }
     }
 
     /**
+     * Create factory instance with providers "array", "form" and "orm".
+     *
+     * @param array $providers Array of provider callables. Each element should
+     *   be of form `['type' => 'a-string', 'callable' => ..]`
+     * @return Cake\View\Form\ContextFactory
+     */
+    public static function createWithDefaults(array $providers = [])
+    {
+        $providers = [
+            [
+                'type' => 'orm',
+                'callable' => function ($request, $data) {
+                    if (is_array($data['entity']) || $data['entity'] instanceof Traversable) {
+                        $pass = (new Collection($data['entity']))->first() !== null;
+                        if ($pass) {
+                            return new EntityContext($request, $data);
+                        }
+                    }
+                    if ($data['entity'] instanceof EntityInterface) {
+                        return new EntityContext($request, $data);
+                    }
+                    if (is_array($data['entity']) && empty($data['entity']['schema'])) {
+                        return new EntityContext($request, $data);
+                    }
+                }
+            ],
+            [
+                'type' => 'array',
+                'callable' => function ($request, $data) {
+                    if (is_array($data['entity']) && isset($data['entity']['schema'])) {
+                        return new ArrayContext($request, $data['entity']);
+                    }
+                }
+            ],
+            [
+                'type' => 'form',
+                'callable' => function ($request, $data) {
+                    if ($data['entity'] instanceof Form) {
+                        return new FormContext($request, $data);
+                    }
+                }
+            ],
+        ] + $providers;
+
+        return new static($providers);
+    }
+
+    /**
      * Add a new context type.
      *
      * Form context types allow FormHelper to interact with
@@ -105,39 +148,4 @@ class ContextFactory
 
         return $context;
     }
-
-    /**
-     * Add the default suite of context providers.
-     *
-     * @return void
-     */
-    protected function addDefaultProviders()
-    {
-        $this->addProvider('orm', function ($request, $data) {
-            if (is_array($data['entity']) || $data['entity'] instanceof Traversable) {
-                $pass = (new Collection($data['entity']))->first() !== null;
-                if ($pass) {
-                    return new EntityContext($request, $data);
-                }
-            }
-            if ($data['entity'] instanceof EntityInterface) {
-                return new EntityContext($request, $data);
-            }
-            if (is_array($data['entity']) && empty($data['entity']['schema'])) {
-                return new EntityContext($request, $data);
-            }
-        });
-
-        $this->addProvider('form', function ($request, $data) {
-            if ($data['entity'] instanceof Form) {
-                return new FormContext($request, $data);
-            }
-        });
-
-        $this->addProvider('array', function ($request, $data) {
-            if (is_array($data['entity']) && isset($data['entity']['schema'])) {
-                return new ArrayContext($request, $data['entity']);
-            }
-        });
-    }
 }

+ 1 - 1
src/View/Helper/FormHelper.php

@@ -282,7 +282,7 @@ class FormHelper extends Helper
     {
         if ($instance === null) {
             if ($this->_contextFactory === null) {
-                $this->_contextFactory = new ContextFactory($contexts);
+                $this->_contextFactory = ContextFactory::createWithDefaults($contexts);
             }
 
             return $this->_contextFactory;