Symfony 3 - 自动生成 BOOLEAN getter 和 setter - isActive 与 getActive
Symfony 3 - Auto generate BOOLEAN getters and setters - isActive vs getActive
自动生成 BOOLEAN getters 和 setter - 不同的输出
Symfony3.2.0: php bin/console 对比 PhpStorm 2016.3
如果我使用命令行 doctrine:generate:entities
或对内部的布尔值使用 PhpStorm 函数 Generate - Getters and Setters
,生成的代码中似乎有 差异 实体 class.
示例: 我已经设置了这个私有变量,下面是生成 Getters/Setters 的 3 个示例,它们的输出都略有不同。
/**
* @var boolean
* @ORM\Column(name="active", type="boolean")
*/
private $active;
# Generated 'getter' from command line = getActive()
# Generated 'getter' from PhpStorm = isActive()
控制台命令: php bin/console doctrine:generate:entities MyBundle:MyEntity
(注意:getActive,return boolean)
/**
* Set active
*
* @param boolean $active
*
* @return MyEntity
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active
*
* @return boolean
*/
public function getActive()
{
return $this->active;
}
在 PhpStorm 中 - 代码 > 生成 (Alt+Insert) > Getters 和 Setters(带有复选框 'Fluent setters' enabled) (注:isActive, return bool)
/**
* @return bool
*/
public function isActive()
{
return $this->active;
}
/**
* @param bool $active
* @return MyEntity
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
和另一个: PhpStorm - 代码 > 生成 (Alt+Insert) > Getters 和 Setters(带有复选框 'Fluent setters' disabled) (注:isActive, return bool, setActive不return $this)
/**
* @return bool
*/
public function isActive()
{
return $this->active;
}
/**
* @param bool $active
*/
public function setActive($active)
{
$this->active = $active;
}
我的问题:
能否以某种方式配置命令行工具 doctrine:generate:entities
以生成 getter 布尔值 自动 作为 is...
而不是 'get...' ? (因此它 always 生成布尔 getter 方法,如:isActive()
、isEnabled()
等)
我看到一些examples/tutorials方法setActive()
没有return$this
,所以不能使用链接。 return $this
是最佳做法吗?什么是首选方式? (当你做 return $this
时,性能可能有缺点吗?)
评论部分中 return 类型的细微差别是否对应用程序有任何影响(使用命令行或其他方式进行数据库迁移)?还是 bool
和 boolean
类型在 Symfony 中以相同的方式处理?
(3.例子)
@return bool (Generated by command line)
vs
@return boolean (Generated by PhpStorm)
我试了一下代码,不幸的是没有办法用现有的设置生成不同的代码。所有 类 都是硬编码的,无法用命令或 Symfony 设置覆盖它。
所以我稍微扩展了生成器 类 并创建了接受生成器作为参数的扩展命令。我还创建了示例生成器,它创建了 'is...' 方法来设置布尔值。
不幸的是,有一些来自现有 类 的复制粘贴,因为无法扩展它。
回答第二个问题我认为使用流畅的界面更符合个人喜好。我是老派开发人员,不习惯 PHP 中的流畅界面。我没有看到它对性能有任何重大影响。
第3题。 bool
和 boolean
的区别在于 bool
是一个 scalar type declaration 而 boolean
是一个变量类型。请参阅文档中的 'Warning'。它解释了很多。
<?php
// src/AppBundle/Command/GenerateDoctrineEntityExtendedCommand.php
namespace AppBundle\Command;
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineEntityCommand;
use Sensio\Bundle\GeneratorBundle\Generator\Generator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class GenerateDoctrineEntityExtendedCommand extends GenerateDoctrineEntityCommand
{
/** @var Generator */
private $generator;
protected function configure()
{
parent::configure();
$this->setName('doctrine:generate:entity:extended');
$this->setDescription($this->getDescription() . " Allows specifying generator class.");
$this->addOption('generator', null, InputOption::VALUE_REQUIRED, "The generator class to create entity.", 'Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator');
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);
if ($class = $input->getOption('generator')) {
if (!class_exists($class)) {
throw new \Exception('Class ' . $class . 'does not exist.');
}
$this->generator = new $class($this->getContainer()->get('filesystem'), $this->getContainer()->get('doctrine'));
}
}
protected function createGenerator()
{
return $this->generator;
}
}
DoctrineEntityGenerator 替换:
<?php
// src/AppBundle/Generator/IsDoctrineEntityGenerator.php
namespace AppBundle\Generator;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator;
class IsDoctrineEntityGenerator extends DoctrineEntityGenerator
{
protected function getEntityGenerator()
{
// This is the place where customized entity generator is instantiated instead of default
$entityGenerator = new IsEntityGenerator();
$entityGenerator->setGenerateAnnotations(false);
$entityGenerator->setGenerateStubMethods(true);
$entityGenerator->setRegenerateEntityIfExists(false);
$entityGenerator->setUpdateEntityIfExists(true);
$entityGenerator->setNumSpaces(4);
$entityGenerator->setAnnotationPrefix('ORM\');
return $entityGenerator;
}
}
EntityGenerator 替换:
<?php
// src/AppBundle/Generator/IsEntityGenerator.php
namespace AppBundle\Generator;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Tools\EntityGenerator;
use Doctrine\DBAL\Types\Type;
class IsEntityGenerator extends EntityGenerator
{
protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null)
{
//
// This is the only line I've added compared to the original method
//
$methodPrefix = ($type == 'get' && $typeHint == 'boolean') ? 'is' : $type;
$methodName = $methodPrefix . Inflector::classify($fieldName);
$variableName = Inflector::camelize($fieldName);
if (in_array($type, array("add", "remove"))) {
$methodName = Inflector::singularize($methodName);
$variableName = Inflector::singularize($variableName);
}
if ($this->hasMethod($methodName, $metadata)) {
return '';
}
$this->staticReflection[$metadata->name]['methods'][] = strtolower($methodName);
$var = sprintf('%sMethodTemplate', $type);
$template = static::$$var;
$methodTypeHint = null;
$types = Type::getTypesMap();
$variableType = $typeHint ? $this->getType($typeHint) : null;
if ($typeHint && !isset($types[$typeHint])) {
$variableType = '\' . ltrim($variableType, '\');
$methodTypeHint = '\' . $typeHint . ' ';
}
$replacements = array(
'<description>' => ucfirst($type) . ' ' . $variableName,
'<methodTypeHint>' => $methodTypeHint,
'<variableType>' => $variableType,
'<variableName>' => $variableName,
'<methodName>' => $methodName,
'<fieldName>' => $fieldName,
'<variableDefault>' => ($defaultValue !== null) ? (' = ' . $defaultValue) : '',
'<entity>' => $this->getClassName($metadata)
);
$method = str_replace(
array_keys($replacements),
array_values($replacements),
$template
);
return $this->prefixCodeWithSpaces($method);
}
}
到目前为止,这恐怕是您想要的唯一选择。
Stepashka 回答正确。
但我认为有一种更好、更简单、更传统的方法可以帮助您在 Symfony 中使用 PhpStorm 自动生成 Getters 和 Setters 方法。
您可以自定义 PhpStorm Getters 和 Setters 生成方式!
您只需前往:
Preferences/Editor/File 和代码 Templates/Code
然后对于 Getter,要将 isActive 修改为 getActive,您必须将 "PHP Getter Method" 更改为:
/*
* @return ${TYPE_HINT}
*/
public ${STATIC} function get${NAME}()#if(${RETURN_TYPE}): ${RETURN_TYPE}#else#end
{
#if (${STATIC} == "static")
return self::$${FIELD_NAME};
#else
return $this->${FIELD_NAME};
#end
}
对于 Setter,要添加 "return $this",您必须将 "PHP Setter Method" 更改为:
/*
* @param ${TYPE_HINT} $${PARAM_NAME}
* @return ${CLASS_NAME}
*/
public ${STATIC} function set${NAME}(#if (${SCALAR_TYPE_HINT})(${SCALAR_TYPE_HINT} #else#end$${PARAM_NAME})
{
#if (${STATIC} == "static")
self::$${FIELD_NAME} = $${PARAM_NAME};
#else
$this->${FIELD_NAME} = $${PARAM_NAME};
#end
return $this;
}
最后,不要忘记应用更改。
自动生成 BOOLEAN getters 和 setter - 不同的输出
Symfony3.2.0: php bin/console 对比 PhpStorm 2016.3
如果我使用命令行 doctrine:generate:entities
或对内部的布尔值使用 PhpStorm 函数 Generate - Getters and Setters
,生成的代码中似乎有 差异 实体 class.
示例: 我已经设置了这个私有变量,下面是生成 Getters/Setters 的 3 个示例,它们的输出都略有不同。
/**
* @var boolean
* @ORM\Column(name="active", type="boolean")
*/
private $active;
# Generated 'getter' from command line = getActive()
# Generated 'getter' from PhpStorm = isActive()
控制台命令: php bin/console doctrine:generate:entities MyBundle:MyEntity
(注意:getActive,return boolean)
/**
* Set active
*
* @param boolean $active
*
* @return MyEntity
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active
*
* @return boolean
*/
public function getActive()
{
return $this->active;
}
在 PhpStorm 中 - 代码 > 生成 (Alt+Insert) > Getters 和 Setters(带有复选框 'Fluent setters' enabled) (注:isActive, return bool)
/**
* @return bool
*/
public function isActive()
{
return $this->active;
}
/**
* @param bool $active
* @return MyEntity
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
和另一个: PhpStorm - 代码 > 生成 (Alt+Insert) > Getters 和 Setters(带有复选框 'Fluent setters' disabled) (注:isActive, return bool, setActive不return $this)
/**
* @return bool
*/
public function isActive()
{
return $this->active;
}
/**
* @param bool $active
*/
public function setActive($active)
{
$this->active = $active;
}
我的问题:
能否以某种方式配置命令行工具
doctrine:generate:entities
以生成 getter 布尔值 自动 作为is...
而不是 'get...' ? (因此它 always 生成布尔 getter 方法,如:isActive()
、isEnabled()
等)我看到一些examples/tutorials方法
setActive()
没有return$this
,所以不能使用链接。 return$this
是最佳做法吗?什么是首选方式? (当你做 return$this
时,性能可能有缺点吗?)评论部分中 return 类型的细微差别是否对应用程序有任何影响(使用命令行或其他方式进行数据库迁移)?还是
bool
和boolean
类型在 Symfony 中以相同的方式处理?
(3.例子)
@return bool (Generated by command line)
vs
@return boolean (Generated by PhpStorm)
我试了一下代码,不幸的是没有办法用现有的设置生成不同的代码。所有 类 都是硬编码的,无法用命令或 Symfony 设置覆盖它。
所以我稍微扩展了生成器 类 并创建了接受生成器作为参数的扩展命令。我还创建了示例生成器,它创建了 'is...' 方法来设置布尔值。
不幸的是,有一些来自现有 类 的复制粘贴,因为无法扩展它。
回答第二个问题我认为使用流畅的界面更符合个人喜好。我是老派开发人员,不习惯 PHP 中的流畅界面。我没有看到它对性能有任何重大影响。
第3题。 bool
和 boolean
的区别在于 bool
是一个 scalar type declaration 而 boolean
是一个变量类型。请参阅文档中的 'Warning'。它解释了很多。
<?php
// src/AppBundle/Command/GenerateDoctrineEntityExtendedCommand.php
namespace AppBundle\Command;
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineEntityCommand;
use Sensio\Bundle\GeneratorBundle\Generator\Generator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class GenerateDoctrineEntityExtendedCommand extends GenerateDoctrineEntityCommand
{
/** @var Generator */
private $generator;
protected function configure()
{
parent::configure();
$this->setName('doctrine:generate:entity:extended');
$this->setDescription($this->getDescription() . " Allows specifying generator class.");
$this->addOption('generator', null, InputOption::VALUE_REQUIRED, "The generator class to create entity.", 'Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator');
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);
if ($class = $input->getOption('generator')) {
if (!class_exists($class)) {
throw new \Exception('Class ' . $class . 'does not exist.');
}
$this->generator = new $class($this->getContainer()->get('filesystem'), $this->getContainer()->get('doctrine'));
}
}
protected function createGenerator()
{
return $this->generator;
}
}
DoctrineEntityGenerator 替换:
<?php
// src/AppBundle/Generator/IsDoctrineEntityGenerator.php
namespace AppBundle\Generator;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator;
class IsDoctrineEntityGenerator extends DoctrineEntityGenerator
{
protected function getEntityGenerator()
{
// This is the place where customized entity generator is instantiated instead of default
$entityGenerator = new IsEntityGenerator();
$entityGenerator->setGenerateAnnotations(false);
$entityGenerator->setGenerateStubMethods(true);
$entityGenerator->setRegenerateEntityIfExists(false);
$entityGenerator->setUpdateEntityIfExists(true);
$entityGenerator->setNumSpaces(4);
$entityGenerator->setAnnotationPrefix('ORM\');
return $entityGenerator;
}
}
EntityGenerator 替换:
<?php
// src/AppBundle/Generator/IsEntityGenerator.php
namespace AppBundle\Generator;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Tools\EntityGenerator;
use Doctrine\DBAL\Types\Type;
class IsEntityGenerator extends EntityGenerator
{
protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null)
{
//
// This is the only line I've added compared to the original method
//
$methodPrefix = ($type == 'get' && $typeHint == 'boolean') ? 'is' : $type;
$methodName = $methodPrefix . Inflector::classify($fieldName);
$variableName = Inflector::camelize($fieldName);
if (in_array($type, array("add", "remove"))) {
$methodName = Inflector::singularize($methodName);
$variableName = Inflector::singularize($variableName);
}
if ($this->hasMethod($methodName, $metadata)) {
return '';
}
$this->staticReflection[$metadata->name]['methods'][] = strtolower($methodName);
$var = sprintf('%sMethodTemplate', $type);
$template = static::$$var;
$methodTypeHint = null;
$types = Type::getTypesMap();
$variableType = $typeHint ? $this->getType($typeHint) : null;
if ($typeHint && !isset($types[$typeHint])) {
$variableType = '\' . ltrim($variableType, '\');
$methodTypeHint = '\' . $typeHint . ' ';
}
$replacements = array(
'<description>' => ucfirst($type) . ' ' . $variableName,
'<methodTypeHint>' => $methodTypeHint,
'<variableType>' => $variableType,
'<variableName>' => $variableName,
'<methodName>' => $methodName,
'<fieldName>' => $fieldName,
'<variableDefault>' => ($defaultValue !== null) ? (' = ' . $defaultValue) : '',
'<entity>' => $this->getClassName($metadata)
);
$method = str_replace(
array_keys($replacements),
array_values($replacements),
$template
);
return $this->prefixCodeWithSpaces($method);
}
}
到目前为止,这恐怕是您想要的唯一选择。
Stepashka 回答正确。
但我认为有一种更好、更简单、更传统的方法可以帮助您在 Symfony 中使用 PhpStorm 自动生成 Getters 和 Setters 方法。
您可以自定义 PhpStorm Getters 和 Setters 生成方式!
您只需前往: Preferences/Editor/File 和代码 Templates/Code
然后对于 Getter,要将 isActive 修改为 getActive,您必须将 "PHP Getter Method" 更改为:
/*
* @return ${TYPE_HINT}
*/
public ${STATIC} function get${NAME}()#if(${RETURN_TYPE}): ${RETURN_TYPE}#else#end
{
#if (${STATIC} == "static")
return self::$${FIELD_NAME};
#else
return $this->${FIELD_NAME};
#end
}
对于 Setter,要添加 "return $this",您必须将 "PHP Setter Method" 更改为:
/*
* @param ${TYPE_HINT} $${PARAM_NAME}
* @return ${CLASS_NAME}
*/
public ${STATIC} function set${NAME}(#if (${SCALAR_TYPE_HINT})(${SCALAR_TYPE_HINT} #else#end$${PARAM_NAME})
{
#if (${STATIC} == "static")
self::$${FIELD_NAME} = $${PARAM_NAME};
#else
$this->${FIELD_NAME} = $${PARAM_NAME};
#end
return $this;
}
最后,不要忘记应用更改。