Skip to content
Permalink
Browse files

Add static method to create context factory with defaults.

  • Loading branch information...
ADmad committed Aug 8, 2017
1 parent a3232ee commit 8ee647f72ddb96e3da441cb27bc57b6678ccbdba
Showing with 51 additions and 43 deletions.
  1. +50 −42 src/View/Form/ContextFactory.php
  2. +1 −1 src/View/Helper/FormHelper.php
@@ -38,19 +38,62 @@ 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.
*
@@ -105,39 +148,4 @@ public function get(ServerRequest $request, array $data = [])
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']);
}
});
}
}
@@ -282,7 +282,7 @@ public function contextFactory(ContextFactory $instance = null, array $contexts
{
if ($instance === null) {
if ($this->_contextFactory === null) {
$this->_contextFactory = new ContextFactory($contexts);
$this->_contextFactory = ContextFactory::createWithDefaults($contexts);
}
return $this->_contextFactory;

0 comments on commit 8ee647f

Please sign in to comment.
You can’t perform that action at this time.