Skip to content

Instantly share code, notes, and snippets.

@fivestar
Created August 22, 2010 17:34
Show Gist options
  • Save fivestar/544032 to your computer and use it in GitHub Desktop.
Save fivestar/544032 to your computer and use it in GitHub Desktop.
<?php
namespace Application\FivestarBundle\Entity;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
use Symfony\Component\Validator\Constraints\AssertType;
use Symfony\Component\Validator\Mapping\ClassMetadata;
class Address
{
private $street;
private $zipCode;
private $city;
public function getStreet()
{
return $this->street;
}
public function setStreet($street)
{
$this->street = $street;
}
public function getZipCode()
{
return $this->zipCode;
}
public function setZipCode($zipCode)
{
$this->zipCode = $zipCode;
}
public function getCity()
{
return $this->city;
}
public function setCity($city)
{
$this->city = $city;
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('street', new MinLength(6));
$metadata->addGetterConstraint('zipCode', new MinLength(7));
$metadata->addGetterConstraint('zipCode', new MaxLength(7));
$metadata->addGetterConstraint('zipCode', new AssertType('numeric'));
$metadata->addGetterConstraint('city', new MinLength(3));
}
}
<?php
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
/**
* fivestarDevDebugProjectContainer
*
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*/
class fivestarDevDebugProjectContainer extends Container
{
// ...
protected function getValidatorService()
{
if (isset($this->shared['validator'])) return $this->shared['validator'];
$class = 'Symfony\\Component\\Validator\\Validator';
$t = array();
$start = microtime(true);
$cmf = $this->getValidator_Mapping_ClassMetadataFactoryService();
$t['getValidator_Mapping_ClassMetadataFactoryService'] = microtime(true);
$vf = $this->getValidator_ValidatorFactoryService();
$t['getValidator_ValidatorFactoryService'] = microtime(true);
$mi = $this->getValidator_MessageInterpolatorService();
$t['getValidator_MessageInterpolatorService'] = microtime(true);
$instance = new $class($cmf, $vf, $mi);
$t['new Validator'] = microtime(true);
foreach ($t as $name => $time) {
var_dump($name, ($time - $start) * 1000);
}
$this->shared['validator'] = $instance;
return $instance;
}
string 'getValidator_Mapping_ClassMetadataFactoryService' (length=48)
float 69.663047790527
string 'getValidator_ValidatorFactoryService' (length=36)
float 83.226203918457
string 'getValidator_MessageInterpolatorService' (length=39)
float 1619.3971633911
string 'new Validator' (length=13)
float 1620.0180053711
string 'do new action' (length=13)
float 0
string 'create user' (length=11)
float 0.46205520629883
string 'do create form' (length=14)
float 0.47016143798828
string 'create validator' (length=16)
float 1535.8281135559
string 'create user form' (length=16)
float 1545.9589958191
string 'embed address form' (length=18)
float 1547.070980072
--------------------
Symfony\Component\Validator\MessageInterpolator\XliffMessageInterpolatorを変更して
ファイルを読み込まないようにしたあと
--------------------
string 'do new action' (length=13)
float 0
string 'create user' (length=11)
float 0.47111511230469
string 'do create form' (length=14)
float 0.47898292541504
string 'create validator' (length=16)
float 3.7810802459717
string 'create user form' (length=16)
float 13.730049133301
string 'embed address form' (length=18)
float 14.841079711914
<?php
namespace Application\FivestarBundle\Entity;
use Symfony\Component\Validator\Constraints\Min;
use Symfony\Component\Validator\Constraints\Max;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
use Symfony\Component\Validator\Constraints\AssertType;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Mapping\ClassMetadata;
class User
{
private $firstName;
private $lastName;
private $email;
private $married = false;
private $age;
private $gender;
private $address;
public function getFirstName()
{
return $this->firstName;
}
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
public function getLastName()
{
return $this->lastName;
}
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function isMarried()
{
return $this->married;
}
public function setMarried($married)
{
$this->married = (bool)$married;
}
public function getAge() {
return $this->age;
}
public function setAge($age)
{
$this->age = $age;
}
public function getGender()
{
return $this->gender;
}
public function setGender($gender)
{
$this->gender = $gender;
}
public function getAddress()
{
return $this->address;
}
public function setAddress($address) {
$this->address = $address;
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('firstName', new MinLength(2));
$metadata->addGetterConstraint('lastName', new MinLength(2));
$metadata->addGetterConstraint('firstName', new NotBlank());
$metadata->addGetterConstraint('lastName', new NotBlank());
$metadata->addGetterConstraint('email', new Email());
$metadata->addGetterConstraint('email', new NotBlank());
$metadata->addGetterConstraint('age', new Min(6));
$metadata->addGetterConstraint('gender', new Choice(array('choices' => self::getGenders())));
$metadata->addGetterConstraint('address', new Valid());
}
public static function getGenders()
{
return array('male', 'female');
}
}
<?php
namespace Application\FivestarBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FieldGroup;
use Symfony\Component\Form\ChoiceField;
use Symfony\Component\Form\TextField;
use Symfony\Component\Form\CheckboxField;
use Symfony\Component\Form\NumberField;
use Symfony\Component\Form\PasswordField;
use Symfony\Component\Form\RepeatedField;
use Application\FivestarBundle\Entity\User;
use Application\FivestarBundle\Entity\Address;
class UserController extends Controller
{
static public $t = array();
public function newAction()
{
self::$t['do new action'] = $start = microtime(true);
$user = new User();
$user->setAddress(new Address());
self::$t['create user'] = microtime(true);
$form = $this->createForm($user);
foreach (self::$t as $name => $time) {
var_dump($name, ($time - $start) * 1000);
}
exit;
return $this->render('FivestarBundle:User:new:twig', array(
'form' => $form,
));
}
protected function createForm($user)
{
self::$t['do create form'] = microtime(true);
$validator = $this['validator'];
self::$t['create validator'] = microtime(true);
$form = new Form('user', $user, $validator);
$form->add(new TextField('firstName'));
$form->add(new TextField('lastName'));
$form->add(new RepeatedField(new TextField('email')));
$form->add(new CheckboxField('married'));
$form->add(new NumberField('age'));
$form->add(new ChoiceField('gender', array('choices' => array_combine(User::getGenders(), User::getGenders()))));
self::$t['create user form'] = microtime(true);
$addressGroup = new FieldGroup('address');
$addressGroup->add(new TextField('street'));
$addressGroup->add(new TextField('zipCode'));
$addressGroup->add(new TextField('city'));
$form->add($addressGroup);
self::$t['embed address form'] = microtime(true);
return $form;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment