Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blast][UIBuilder] Add UI Builder experiments #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/Blast/Component/UIBuilder/Builder/AbstractBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/*
* Copyright (C) 2015-2017 Libre Informatique
*
* This file is licenced under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blast\Component\UIBuilder\Builder;

use Blast\Component\UIBuilder\Factory\AbstractFactoryInterface;
use Blast\Component\UIBuilder\Model\UiModelInterface;

abstract class AbstractBuilder implements BuilderInterface
{
/**
* @var string
*/
protected $name;
/**
* @var UiModelInterface
*/
protected $model;

/**
* @var BuilderInterface
*/
protected $parent;

/**
* @var AbstractFactoryInterface
*/
protected $abstractFactory;

/**
* @var array
*/
protected $options = [];

public function __construct(?BuilderInterface $parent, AbstractFactoryInterface $abstractFactory, string $name)
{
$this->parent = $parent;
$this->abstractFactory = $abstractFactory;
$this->name = $name;
}

public function getOptions()
{
return $this->options;
}

/**
* @return AbstractFactoryInterface
*/
public function getAbstractFactory()
{
return $this->abstractFactory;
}

/**
* @return BuilderInterface
*/
public function getParent()
{
return $this->parent;
}

/**
* @return ModelInterface
*/
public function getParentModel()
{
return $this->getParent()->getModel();
}

protected function findChildModelByName(string $name)
{
return $this->getModel()->findChildByName($name);
}

/**
* @return UiModelInterface
*/
public function getModel(): UiModelInterface
{
if (null === $this->model) {
$this->model = $this->build();
}

return $this->model;
}

public function setModel(UiModelInterface $model)
{
$this->model = $model;
}

protected function addInParentModel()
{
$this->getParentModel()->addChild($this->getModel());
}

/**
* @return BuilderInterface
*/
public function end()
{
if (null !== $this->parent) {
$this->addInParentModel();

return $this->getParent();
} else {
return $this;
}
}

abstract protected function build();
}
55 changes: 55 additions & 0 deletions src/Blast/Component/UIBuilder/Builder/BaseBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* Copyright (C) 2015-2017 Libre Informatique
*
* This file is licenced under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blast\Component\UIBuilder\Builder;

abstract class BaseBuilder extends AbstractBuilder
{
public function css(string $classes)
{
$this->options['css.classes'] = $classes;

return $this;
}

public function containerCss(string $classes)
{
$this->options['container_css.classes'] = $classes;

return $this;
}

public function template(string $template)
{
$this->options['template'] = $template;

return $this;
}

public function useBuilder(self $builder)
{
$this->model = $builder->getModel();
$this->options = $builder->getOptions();
$this->model->setName($this->name);

return $this;
}

public function override(string $childName)
{
$childModel = $this->getModel()->findChildByName($childName, true);
if (null === $childModel) {
throw new \InvalidArgumentException('child named "' . $childName . '" does not exist');
}
$builderClass = BuilderClassMap::getBuilderClassFromModel($childModel);

return (new BuilderOverride($this, $builderClass, $childModel))->getBuilder();
}
}
24 changes: 24 additions & 0 deletions src/Blast/Component/UIBuilder/Builder/BuilderClassMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* Copyright (C) 2015-2017 Libre Informatique
*
* This file is licenced under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blast\Component\UIBuilder\Builder;

use Blast\Component\UIBuilder\Model\UiModelInterface;

class BuilderClassMap
{
public static function getBuilderClassFromModel(UiModelInterface $model): string
{
$modelClass = (new \ReflectionClass($model))->getShortName();
$builderClass = __NAMESPACE__ . '\\' . $modelClass . 'Builder';

return $builderClass;
}
}
20 changes: 20 additions & 0 deletions src/Blast/Component/UIBuilder/Builder/BuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
*
* Copyright (C) 2015-2017 Libre Informatique
*
* This file is licenced under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blast\Component\UIBuilder\Builder;

interface BuilderInterface
{
/**
* @return BuilderInterface
*/
public function end();
}
50 changes: 50 additions & 0 deletions src/Blast/Component/UIBuilder/Builder/BuilderOverride.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* Copyright (C) 2015-2017 Libre Informatique
*
* This file is licenced under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blast\Component\UIBuilder\Builder;

use Blast\Component\UIBuilder\Model\UiModelInterface;

class BuilderOverride implements BuilderInterface, UiModelInterface
{
protected $model;
protected $builder;

public function __construct(BuilderInterface $rootBuilder, $builderClass, UiModelInterface $model)
{
$this->builder = new $builderClass(null, $rootBuilder->getAbstractFactory(), $model->getName());
$this->builder->setModel($model);
$this->model = $model;
}

public function getName(): string
{
return $this->model->getName();
}

public function getBuilder(): BuilderInterface
{
return $this->builder;
}

public function getModel(): UiModelInterface
{
return $this;
}

public function addChild($child)
{
$this->model->mergeWith($child);
}

public function end()
{
}
}
19 changes: 19 additions & 0 deletions src/Blast/Component/UIBuilder/Builder/CollectionAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* Copyright (C) 2015-2017 Libre Informatique
*
* This file is licenced under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blast\Component\UIBuilder\Builder;

trait CollectionAwareTrait
{
public function Collection($name)
{
return new CollectionBuilder($this, $this->getAbstractFactory(), $name);
}
}
35 changes: 35 additions & 0 deletions src/Blast/Component/UIBuilder/Builder/CollectionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright (C) 2015-2017 Libre Informatique
*
* This file is licenced under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blast\Component\UIBuilder\Builder;

class CollectionBuilder extends GroupBuilder
{
use FieldAwareTrait;

public function type($type)
{
$this->options['type'] = $type;

return $this;
}

public function valueAccessor($valueAccessor)
{
$this->options['valueAccessor'] = $valueAccessor;

return $this;
}

public function build()
{
return $this->getAbstractFactory()->createCollection($this->name, $this->options);
}
}
24 changes: 24 additions & 0 deletions src/Blast/Component/UIBuilder/Builder/FieldAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* Copyright (C) 2015-2017 Libre Informatique
*
* This file is licenced under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blast\Component\UIBuilder\Builder;

trait FieldAwareTrait
{
public function field($name)
{
return new FieldBuilder($this, $this->getAbstractFactory(), $name);
}

public function inputField($name)
{
return new InputFieldBuilder($this, $this->getAbstractFactory(), $name);
}
}
Loading