Refactored Form.

This commit is contained in:
Dmitry Khomutov 2018-03-04 17:50:08 +07:00
parent 1fdf9a7ab1
commit cfe93434ad
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
45 changed files with 58 additions and 83 deletions

View file

@ -30,7 +30,6 @@ require_once(ROOT_DIR . 'vendor/autoload.php');
$conf = [];
$conf['b8']['app']['namespace'] = 'PHPCensor';
$conf['b8']['app']['default_controller'] = 'Home';
$conf['b8']['view']['path'] = SRC_DIR . 'View/';
$config = new PHPCensor\Config($conf);

View file

@ -1,9 +0,0 @@
<?php
namespace b8\Form\Element;
use b8\Form\FieldSet;
class CheckboxGroup extends FieldSet
{
}

View file

@ -1,9 +0,0 @@
<?php
namespace b8\Form\Element;
use b8\Form\Input;
class Hidden extends Input
{
}

View file

@ -4,10 +4,6 @@ namespace PHPCensor;
use Symfony\Component\Yaml\Parser as YamlParser;
if (!defined('B8_PATH')) {
define('B8_PATH', dirname(__DIR__) . '/B8Framework/');
}
class Config
{
/**

View file

@ -2,8 +2,7 @@
namespace PHPCensor\Controller;
use b8;
use b8\Form;
use PHPCensor\Form;
use PHPCensor\Controller;
use PHPCensor\Http\Response\RedirectResponse;
use PHPCensor\Model\ProjectGroup;

View file

@ -3,7 +3,7 @@
namespace PHPCensor\Controller;
use PHPCensor\Exception\HttpException\NotFoundException;
use b8\Form;
use PHPCensor\Form;
use JasonGrimes\Paginator;
use PHPCensor;
use PHPCensor\BuildFactory;

View file

@ -2,7 +2,6 @@
namespace PHPCensor\Controller;
use b8;
use PHPCensor\Helper\Email;
use PHPCensor\Helper\Lang;
use PHPCensor\Controller;
@ -117,42 +116,42 @@ class SessionController extends Controller
}
}
$form = new b8\Form();
$form = new \PHPCensor\Form();
$form->setMethod('POST');
$form->setAction(APP_URL . 'session/login');
$email = new b8\Form\Element\Text('email');
$email = new \PHPCensor\Form\Element\Text('email');
$email->setLabel(Lang::get('login'));
$email->setRequired(true);
$email->setContainerClass('form-group');
$email->setClass('form-control');
$form->addField($email);
$pwd = new b8\Form\Element\Password('password');
$pwd = new \PHPCensor\Form\Element\Password('password');
$pwd->setLabel(Lang::get('password'));
$pwd->setRequired(true);
$pwd->setContainerClass('form-group');
$pwd->setClass('form-control');
$form->addField($pwd);
$remember = b8\Form\Element\Checkbox::create('remember_me', Lang::get('remember_me'), false);
$remember = \PHPCensor\Form\Element\Checkbox::create('remember_me', Lang::get('remember_me'), false);
$remember->setContainerClass('form-group');
$remember->setCheckedValue(1);
$remember->setValue(0);
$form->addField($remember);
$pwd = new b8\Form\Element\Submit();
$pwd = new \PHPCensor\Form\Element\Submit();
$pwd->setValue(Lang::get('log_in'));
$pwd->setClass('btn-success');
$form->addField($pwd);
$tokenValue = $this->generateToken();
$_SESSION['login_token'] = $tokenValue;
$token = new b8\Form\Element\Hidden('token');
$token = new \PHPCensor\Form\Element\Hidden('token');
$token->setValue($tokenValue);
$form->addField($token);
$this->view->form = $form->render();
$this->view->form = $form->render();
$this->view->failed = $isLoginFailure;
return $this->view->render();

View file

@ -4,7 +4,7 @@ namespace PHPCensor\Controller;
use PHPCensor\Config;
use PHPCensor\Exception\HttpException\NotFoundException;
use b8\Form;
use PHPCensor\Form;
use PHPCensor\Controller;
use PHPCensor\Helper\Lang;
use PHPCensor\Http\Response\RedirectResponse;

View file

@ -1,9 +1,8 @@
<?php
namespace b8;
namespace PHPCensor;
use b8\Form\FieldSet;
use PHPCensor\View;
use PHPCensor\Form\FieldSet;
class Form extends FieldSet
{

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form;
namespace PHPCensor\Form;
class ControlGroup extends FieldSet
{

View file

@ -1,9 +1,8 @@
<?php
namespace b8\Form;
namespace PHPCensor\Form;
use PHPCensor\View;
use PHPCensor\Config;
abstract class Element
{
@ -168,7 +167,7 @@ abstract class Element
*/
public function render($viewFile = null)
{
$viewPath = Config::getInstance()->get('b8.view.path');
$viewPath = SRC_DIR . 'View/';
if (is_null($viewFile)) {
$class = explode('\\', get_called_class());
@ -178,7 +177,7 @@ abstract class Element
if (file_exists($viewPath . 'Form/' . $viewFile . '.phtml')) {
$view = new View('Form/' . $viewFile);
} else {
$view = new View($viewFile, B8_PATH . 'Form/View/');
$view = new View($viewFile, SRC_DIR . 'Form/View/');
}
$view->name = $this->getName();

View file

@ -1,8 +1,8 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use b8\Form\Input;
use PHPCensor\Form\Input;
use PHPCensor\View;
class Button extends Input

View file

@ -1,9 +1,9 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use PHPCensor\View;
use b8\Form\Input;
use PHPCensor\Form\Input;
class Checkbox extends Input
{

View file

@ -0,0 +1,9 @@
<?php
namespace PHPCensor\Form\Element;
use PHPCensor\Form\FieldSet;
class CheckboxGroup extends FieldSet
{
}

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use PHPCensor\View;

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use PHPCensor\View;

View file

@ -0,0 +1,9 @@
<?php
namespace PHPCensor\Form\Element;
use PHPCensor\Form\Input;
class Hidden extends Input
{
}

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use PHPCensor\View;

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
class Radio extends Select
{

View file

@ -1,9 +1,9 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use PHPCensor\View;
use b8\Form\Input;
use PHPCensor\Form\Input;
class Select extends Input
{

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use PHPCensor\View;

View file

@ -1,8 +1,8 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use b8\Form\Input;
use PHPCensor\Form\Input;
use PHPCensor\View;
class Text extends Input

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use PHPCensor\View;

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form\Element;
namespace PHPCensor\Form\Element;
use PHPCensor\View;

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form;
namespace PHPCensor\Form;
use PHPCensor\View;

View file

@ -1,6 +1,6 @@
<?php
namespace b8\Form;
namespace PHPCensor\Form;
use PHPCensor\View;

View file

@ -1,4 +1,4 @@
<?php if (!($parent instanceof \b8\Form\Element\CheckboxGroup)): ?>
<?php if (!($parent instanceof \PHPCensor\Form\Element\CheckboxGroup)): ?>
<div class="control-group <?= $containerClass ?> <?= (isset($error) ? 'error' : ''); ?>">
<div class="controls">
<div class="checkbox">
@ -13,7 +13,7 @@
<?php if (isset($error)): ?>
<span class="help-block"><?= $error; ?></span>
<?php endif; ?>
<?php if (!($parent instanceof \b8\Form\Element\CheckboxGroup)): ?>
<?php if (!($parent instanceof \PHPCensor\Form\Element\CheckboxGroup)): ?>
</div>
</div>
</div>

View file

@ -28,7 +28,7 @@ class View
protected static function getViewFile($file, $path = null)
{
$viewPath = is_null($path) ? Config::getInstance()->get('b8.view.path') : $path;
$viewPath = is_null($path) ? (SRC_DIR . 'View/') : $path;
$fullPath = $viewPath . $file . '.' . static::$extension;
return $fullPath;

View file

@ -1,8 +1,8 @@
<?php
namespace Tests\b8;
namespace Tests\PHPCensor;
use b8\Form;
use PHPCensor\Form;
use PHPCensor\Config;
class FormTest extends \PHPUnit\Framework\TestCase
@ -15,20 +15,6 @@ class FormTest extends \PHPUnit\Framework\TestCase
self::assertTrue($f->getAction() == '/');
self::assertTrue($f->getMethod() == 'POST');
new Config([
'b8' => [
'view' => [
'path' => __DIR__ . '/data/view/'
]
]
]);
self::assertTrue($f->render('form') == '/POST');
Config::getInstance()->set('b8.view.path', '');
self::assertTrue(strpos((string)$f, '<form') !== false);
}
public function testElementBasics()

View file

@ -1,11 +1,10 @@
<?php
namespace Tests\b8;
namespace Tests\PHPCensor;
use PHPCensor\View;
use PHPUnit\Framework\TestCase;
class ViewTest extends TestCase
class ViewTest extends \PHPUnit\Framework\TestCase
{
public function testSimpleView()
{

View file

@ -30,7 +30,6 @@ $conf = [];
$conf['b8']['app']['namespace'] = 'PHPCensor';
$conf['b8']['app']['default_controller'] = 'Home';
$conf['b8']['view']['path'] = SRC_DIR . 'View/';
$conf['php-censor']['url'] = 'http://php-censor.local';
$config = new PHPCensor\Config($conf);