2010-03-19 00:38:42 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2010-03-19 00:38:42 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools;
|
|
|
|
|
2012-10-12 15:53:20 +04:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
|
|
|
use Doctrine\Common\Util\Inflector;
|
|
|
|
use Doctrine\DBAL\Types\Type;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic class used to generate PHP5 entity classes from ClassMetadataInfo instances
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $classes = $em->getClassMetadataFactory()->getAllMetadata();
|
|
|
|
*
|
|
|
|
* $generator = new \Doctrine\ORM\Tools\EntityGenerator();
|
|
|
|
* $generator->setGenerateAnnotations(true);
|
|
|
|
* $generator->setGenerateStubMethods(true);
|
|
|
|
* $generator->setRegenerateEntityIfExists(false);
|
|
|
|
* $generator->setUpdateEntityIfExists(true);
|
|
|
|
* $generator->generate($classes, '/path/to/generate/entities');
|
|
|
|
*
|
2012-06-06 10:44:25 +04:00
|
|
|
*
|
2010-03-19 00:38:42 +03:00
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
|
|
|
class EntityGenerator
|
|
|
|
{
|
2012-01-13 14:24:35 +04:00
|
|
|
/**
|
|
|
|
* Specifies class fields should be protected
|
|
|
|
*/
|
|
|
|
const FIELD_VISIBLE_PROTECTED = 'protected';
|
2012-04-01 20:29:49 +04:00
|
|
|
|
2012-01-13 14:24:35 +04:00
|
|
|
/**
|
|
|
|
* Specifies class fields should be private
|
|
|
|
*/
|
|
|
|
const FIELD_VISIBLE_PRIVATE = 'private';
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2012-04-01 20:29:49 +04:00
|
|
|
private $backupExisting = true;
|
2011-02-03 01:21:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* The extension to use for written php files
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $extension = '.php';
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* Whether or not the current ClassMetadataInfo instance is new or old
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $isNew = true;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $staticReflection = array();
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* Number of spaces to use for indention in generated code
|
|
|
|
*/
|
|
|
|
private $numSpaces = 4;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* The actual spaces to use for indention
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $spaces = ' ';
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* The class all generated entities should extend
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $classToExtend;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* Whether or not to generation annotations
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $generateAnnotations = false;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-04-01 20:29:49 +04:00
|
|
|
private $annotationsPrefix = '';
|
2011-02-03 01:21:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* Whether or not to generated sub methods
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $generateEntityStubMethods = false;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* Whether or not to update the entity class if it exists already
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $updateEntityIfExists = false;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* Whether or not to re-generate entity class if it exists already
|
2012-04-01 21:59:21 +04:00
|
|
|
*
|
2012-04-01 20:29:49 +04:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $regenerateEntityIfExists = false;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $fieldVisibility = 'private';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash-map for handle types
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $typeAlias = array(
|
|
|
|
Type::DATETIMETZ => '\DateTime',
|
|
|
|
Type::DATETIME => '\DateTime',
|
|
|
|
Type::DATE => '\DateTime',
|
|
|
|
Type::TIME => '\DateTime',
|
|
|
|
Type::OBJECT => '\stdClass',
|
|
|
|
Type::BIGINT => 'integer',
|
|
|
|
Type::SMALLINT => 'integer',
|
|
|
|
Type::TEXT => 'string',
|
|
|
|
Type::BLOB => 'string',
|
2012-04-01 22:09:23 +04:00
|
|
|
Type::DECIMAL => 'float',
|
2012-07-27 12:34:59 +04:00
|
|
|
Type::JSON_ARRAY => 'array',
|
|
|
|
Type::SIMPLE_ARRAY => 'array',
|
2012-04-01 20:29:49 +04:00
|
|
|
);
|
2012-01-13 14:24:35 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $classTemplate =
|
2010-03-19 00:38:42 +03:00
|
|
|
'<?php
|
|
|
|
|
2010-08-28 18:29:08 +04:00
|
|
|
<namespace>
|
2010-03-20 01:47:34 +03:00
|
|
|
|
2011-05-25 14:35:54 +04:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
<entityAnnotation>
|
|
|
|
<entityClassName>
|
|
|
|
{
|
|
|
|
<entityBody>
|
2012-03-02 12:57:24 +04:00
|
|
|
}
|
|
|
|
';
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $getMethodTemplate =
|
2010-03-20 01:38:45 +03:00
|
|
|
'/**
|
2010-03-20 01:47:34 +03:00
|
|
|
* <description>
|
|
|
|
*
|
2011-06-28 23:24:24 +04:00
|
|
|
* @return <variableType>
|
2010-03-20 01:47:34 +03:00
|
|
|
*/
|
2010-03-20 01:38:45 +03:00
|
|
|
public function <methodName>()
|
|
|
|
{
|
2010-04-14 19:21:39 +04:00
|
|
|
<spaces>return $this-><fieldName>;
|
2010-03-20 01:38:45 +03:00
|
|
|
}';
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $setMethodTemplate =
|
2010-03-20 01:38:45 +03:00
|
|
|
'/**
|
2010-03-20 01:47:34 +03:00
|
|
|
* <description>
|
|
|
|
*
|
|
|
|
* @param <variableType>$<variableName>
|
2011-09-20 16:59:32 +04:00
|
|
|
* @return <entity>
|
2010-03-20 01:47:34 +03:00
|
|
|
*/
|
2011-10-19 11:15:41 +04:00
|
|
|
public function <methodName>(<methodTypeHint>$<variableName><variableDefault>)
|
2010-03-20 01:38:45 +03:00
|
|
|
{
|
2010-04-14 19:21:39 +04:00
|
|
|
<spaces>$this-><fieldName> = $<variableName>;
|
2012-07-03 14:57:14 +04:00
|
|
|
|
2011-09-20 16:59:32 +04:00
|
|
|
<spaces>return $this;
|
2010-03-20 01:38:45 +03:00
|
|
|
}';
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $addMethodTemplate =
|
2010-03-20 01:38:45 +03:00
|
|
|
'/**
|
2010-03-20 01:47:34 +03:00
|
|
|
* <description>
|
|
|
|
*
|
|
|
|
* @param <variableType>$<variableName>
|
2012-01-24 22:08:25 +04:00
|
|
|
* @return <entity>
|
2010-03-20 01:47:34 +03:00
|
|
|
*/
|
2010-03-20 01:38:45 +03:00
|
|
|
public function <methodName>(<methodTypeHint>$<variableName>)
|
|
|
|
{
|
2010-04-14 19:21:39 +04:00
|
|
|
<spaces>$this-><fieldName>[] = $<variableName>;
|
2012-07-03 14:57:14 +04:00
|
|
|
|
2012-01-24 22:08:25 +04:00
|
|
|
<spaces>return $this;
|
2012-07-04 23:03:50 +04:00
|
|
|
}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $removeMethodTemplate =
|
|
|
|
'/**
|
|
|
|
* <description>
|
|
|
|
*
|
2012-07-04 23:30:25 +04:00
|
|
|
* @param <variableType>$<variableName>
|
2012-07-04 23:03:50 +04:00
|
|
|
*/
|
|
|
|
public function <methodName>(<methodTypeHint>$<variableName>)
|
|
|
|
{
|
|
|
|
<spaces>$this-><fieldName>->removeElement($<variableName>);
|
2010-03-20 01:38:45 +03:00
|
|
|
}';
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $lifecycleCallbackMethodTemplate =
|
2010-03-20 01:38:45 +03:00
|
|
|
'/**
|
2010-03-20 01:47:34 +03:00
|
|
|
* @<name>
|
|
|
|
*/
|
2010-03-20 01:38:45 +03:00
|
|
|
public function <methodName>()
|
|
|
|
{
|
2010-04-14 19:21:39 +04:00
|
|
|
<spaces>// Add your code here
|
2010-03-20 01:38:45 +03:00
|
|
|
}';
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $constructorMethodTemplate =
|
2012-06-06 10:44:25 +04:00
|
|
|
'/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct()
|
2011-02-03 01:30:16 +03:00
|
|
|
{
|
|
|
|
<spaces><collections>
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
2011-05-25 14:35:54 +04:00
|
|
|
public function __construct()
|
|
|
|
{
|
2011-10-10 21:20:01 +04:00
|
|
|
if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->annotationsPrefix = 'ORM\\';
|
2011-05-25 14:35:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
/**
|
|
|
|
* Generate and write entity classes for the given array of ClassMetadataInfo instances
|
|
|
|
*
|
|
|
|
* @param array $metadatas
|
2011-05-25 14:35:54 +04:00
|
|
|
* @param string $outputDirectory
|
2010-03-19 00:38:42 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function generate(array $metadatas, $outputDirectory)
|
|
|
|
{
|
|
|
|
foreach ($metadatas as $metadata) {
|
|
|
|
$this->writeEntityClass($metadata, $outputDirectory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generated and write entity class to disk for the given ClassMetadataInfo instance
|
|
|
|
*
|
|
|
|
* @param ClassMetadataInfo $metadata
|
2011-05-25 14:35:54 +04:00
|
|
|
* @param string $outputDirectory
|
2010-03-19 00:38:42 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function writeEntityClass(ClassMetadataInfo $metadata, $outputDirectory)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->extension;
|
2010-03-19 00:38:42 +03:00
|
|
|
$dir = dirname($path);
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if ( ! is_dir($dir)) {
|
|
|
|
mkdir($dir, 0777, true);
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->isNew = !file_exists($path) || (file_exists($path) && $this->regenerateEntityIfExists);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ( ! $this->isNew) {
|
|
|
|
$this->parseTokensInEntityFile(file_get_contents($path));
|
2012-01-12 14:20:49 +04:00
|
|
|
} else {
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->staticReflection[$metadata->name] = array('properties' => array(), 'methods' => array());
|
2011-02-03 01:21:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->backupExisting && file_exists($path)) {
|
2011-03-15 15:39:38 +03:00
|
|
|
$backupPath = dirname($path) . DIRECTORY_SEPARATOR . basename($path) . "~";
|
2011-02-03 01:21:42 +03:00
|
|
|
if (!copy($path, $backupPath)) {
|
2011-12-04 00:19:21 +04:00
|
|
|
throw new \RuntimeException("Attempt to backup overwritten entity file but copy operation failed.");
|
2011-02-03 01:21:42 +03:00
|
|
|
}
|
2010-03-19 01:35:16 +03:00
|
|
|
}
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
// If entity doesn't exist or we're re-generating the entities entirely
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->isNew) {
|
2010-03-19 00:38:42 +03:00
|
|
|
file_put_contents($path, $this->generateEntityClass($metadata));
|
|
|
|
// If entity exists and we're allowed to update the entity class
|
2012-10-22 03:58:31 +04:00
|
|
|
} elseif ( ! $this->isNew && $this->updateEntityIfExists) {
|
2010-03-19 00:38:42 +03:00
|
|
|
file_put_contents($path, $this->generateUpdatedEntityClass($metadata, $path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance
|
|
|
|
*
|
2011-05-25 14:35:54 +04:00
|
|
|
* @param ClassMetadataInfo $metadata
|
2010-03-19 00:38:42 +03:00
|
|
|
* @return string $code
|
|
|
|
*/
|
|
|
|
public function generateEntityClass(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
$placeHolders = array(
|
|
|
|
'<namespace>',
|
|
|
|
'<entityAnnotation>',
|
|
|
|
'<entityClassName>',
|
2010-04-14 22:17:55 +04:00
|
|
|
'<entityBody>'
|
2010-03-19 00:38:42 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$replacements = array(
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->generateEntityNamespace($metadata),
|
|
|
|
$this->generateEntityDocBlock($metadata),
|
|
|
|
$this->generateEntityClassName($metadata),
|
|
|
|
$this->generateEntityBody($metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
);
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$code = str_replace($placeHolders, $replacements, self::$classTemplate);
|
2012-10-22 03:58:31 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
return str_replace('<spaces>', $this->spaces, $code);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the updated code for the given ClassMetadataInfo and entity at path
|
|
|
|
*
|
2011-05-25 14:35:54 +04:00
|
|
|
* @param ClassMetadataInfo $metadata
|
|
|
|
* @param string $path
|
2010-03-19 00:38:42 +03:00
|
|
|
* @return string $code;
|
|
|
|
*/
|
|
|
|
public function generateUpdatedEntityClass(ClassMetadataInfo $metadata, $path)
|
|
|
|
{
|
2010-03-19 01:35:16 +03:00
|
|
|
$currentCode = file_get_contents($path);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$body = $this->generateEntityBody($metadata);
|
|
|
|
$body = str_replace('<spaces>', $this->spaces, $body);
|
2010-03-19 01:35:16 +03:00
|
|
|
$last = strrpos($currentCode, '}');
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-10-31 02:42:01 +04:00
|
|
|
return substr($currentCode, 0, $last) . $body . (strlen($body) > 0 ? "\n" : ''). "}\n";
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the number of spaces the exported class should have
|
|
|
|
*
|
2011-05-25 14:35:54 +04:00
|
|
|
* @param integer $numSpaces
|
2010-03-19 00:38:42 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setNumSpaces($numSpaces)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->spaces = str_repeat(' ', $numSpaces);
|
|
|
|
$this->numSpaces = $numSpaces;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the extension to use when writing php files to disk
|
|
|
|
*
|
2011-05-25 14:35:54 +04:00
|
|
|
* @param string $extension
|
2010-03-19 00:38:42 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setExtension($extension)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->extension = $extension;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the name of the class the generated classes should extend from
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setClassToExtend($classToExtend)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->classToExtend = $classToExtend;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether or not to generate annotations for the entity
|
|
|
|
*
|
2011-05-25 14:35:54 +04:00
|
|
|
* @param bool $bool
|
2010-03-19 00:38:42 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setGenerateAnnotations($bool)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->generateAnnotations = $bool;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-01-06 19:58:27 +04:00
|
|
|
/**
|
2012-01-13 14:24:35 +04:00
|
|
|
* Set the class fields visibility for the entity (can either be private or protected)
|
2012-01-06 19:58:27 +04:00
|
|
|
*
|
|
|
|
* @param bool $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setFieldVisibility($visibility)
|
|
|
|
{
|
2012-01-13 17:43:13 +04:00
|
|
|
if ($visibility !== self::FIELD_VISIBLE_PRIVATE && $visibility !== self::FIELD_VISIBLE_PROTECTED) {
|
2012-01-13 14:24:35 +04:00
|
|
|
throw new \InvalidArgumentException('Invalid provided visibilty (only private and protected are allowed): ' . $visibility);
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->fieldVisibility = $visibility;
|
2012-01-06 19:58:27 +04:00
|
|
|
}
|
2012-01-13 14:24:35 +04:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
/**
|
|
|
|
* Set an annotation prefix.
|
|
|
|
*
|
|
|
|
* @param string $prefix
|
|
|
|
*/
|
|
|
|
public function setAnnotationPrefix($prefix)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->annotationsPrefix = $prefix;
|
2011-02-03 01:21:42 +03:00
|
|
|
}
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
/**
|
|
|
|
* Set whether or not to try and update the entity if it already exists
|
|
|
|
*
|
2011-05-25 14:35:54 +04:00
|
|
|
* @param bool $bool
|
2010-03-19 00:38:42 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUpdateEntityIfExists($bool)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->updateEntityIfExists = $bool;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether or not to regenerate the entity if it exists
|
|
|
|
*
|
|
|
|
* @param bool $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setRegenerateEntityIfExists($bool)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->regenerateEntityIfExists = $bool;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether or not to generate stub methods for the entity
|
|
|
|
*
|
|
|
|
* @param bool $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setGenerateStubMethods($bool)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->generateEntityStubMethods = $bool;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
/**
|
|
|
|
* Should an existing entity be backed up if it already exists?
|
|
|
|
*/
|
|
|
|
public function setBackupExisting($bool)
|
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->backupExisting = $bool;
|
2011-02-03 01:21:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 21:59:21 +04:00
|
|
|
/**
|
2012-04-01 20:29:49 +04:00
|
|
|
* @param string $type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getType($type)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
if (isset($this->typeAlias[$type])) {
|
|
|
|
return $this->typeAlias[$type];
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2012-04-01 20:29:49 +04:00
|
|
|
|
|
|
|
return $type;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityNamespace(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->hasNamespace($metadata)) {
|
|
|
|
return 'namespace ' . $this->getNamespace($metadata) .';';
|
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityClassName(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
return 'class ' . $this->getClassName($metadata) .
|
|
|
|
($this->extendsClass() ? ' extends ' . $this->getClassToExtendName() : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateEntityBody(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
$fieldMappingProperties = $this->generateEntityFieldMappingProperties($metadata);
|
|
|
|
$associationMappingProperties = $this->generateEntityAssociationMappingProperties($metadata);
|
|
|
|
$stubMethods = $this->generateEntityStubMethods ? $this->generateEntityStubMethods($metadata) : null;
|
|
|
|
$lifecycleCallbackMethods = $this->generateEntityLifecycleCallbackMethods($metadata);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-29 02:56:59 +04:00
|
|
|
$code = array();
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if ($fieldMappingProperties) {
|
2010-03-29 02:56:59 +04:00
|
|
|
$code[] = $fieldMappingProperties;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if ($associationMappingProperties) {
|
2010-03-29 02:56:59 +04:00
|
|
|
$code[] = $associationMappingProperties;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$code[] = $this->generateEntityConstructor($metadata);
|
2011-02-03 01:30:16 +03:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if ($stubMethods) {
|
2010-03-29 02:56:59 +04:00
|
|
|
$code[] = $stubMethods;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if ($lifecycleCallbackMethods) {
|
2010-03-29 02:56:59 +04:00
|
|
|
$code[] = $lifecycleCallbackMethods;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
|
|
|
return implode("\n", $code);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityConstructor(ClassMetadataInfo $metadata)
|
2011-02-03 01:30:16 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->hasMethod('__construct', $metadata)) {
|
2011-02-03 01:30:16 +03:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$collections = array();
|
2011-10-03 09:07:07 +04:00
|
|
|
|
2012-01-12 23:03:31 +04:00
|
|
|
foreach ($metadata->associationMappings as $mapping) {
|
2011-02-03 01:30:16 +03:00
|
|
|
if ($mapping['type'] & ClassMetadataInfo::TO_MANY) {
|
|
|
|
$collections[] = '$this->'.$mapping['fieldName'].' = new \Doctrine\Common\Collections\ArrayCollection();';
|
|
|
|
}
|
|
|
|
}
|
2011-12-04 00:19:21 +04:00
|
|
|
|
2011-02-03 01:30:16 +03:00
|
|
|
if ($collections) {
|
2012-04-01 20:29:49 +04:00
|
|
|
return $this->prefixCodeWithSpaces(str_replace("<collections>", implode("\n".$this->spaces, $collections), self::$constructorMethodTemplate));
|
2011-02-03 01:30:16 +03:00
|
|
|
}
|
2011-12-04 00:19:21 +04:00
|
|
|
|
2011-02-03 01:30:16 +03:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
/**
|
|
|
|
* @todo this won't work if there is a namespace in brackets and a class outside of it.
|
2011-04-30 12:42:38 +04:00
|
|
|
* @param string $src
|
2011-02-03 01:21:42 +03:00
|
|
|
*/
|
2012-04-01 20:29:49 +04:00
|
|
|
private function parseTokensInEntityFile($src)
|
2011-02-03 01:21:42 +03:00
|
|
|
{
|
2011-04-30 12:42:38 +04:00
|
|
|
$tokens = token_get_all($src);
|
2011-02-03 01:21:42 +03:00
|
|
|
$lastSeenNamespace = "";
|
|
|
|
$lastSeenClass = false;
|
2011-05-25 14:35:54 +04:00
|
|
|
|
2011-04-30 12:42:38 +04:00
|
|
|
$inNamespace = false;
|
|
|
|
$inClass = false;
|
2012-10-22 03:58:31 +04:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
for ($i = 0; $i < count($tokens); $i++) {
|
|
|
|
$token = $tokens[$i];
|
2011-04-30 12:42:38 +04:00
|
|
|
if (in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($inNamespace) {
|
|
|
|
if ($token[0] == T_NS_SEPARATOR || $token[0] == T_STRING) {
|
|
|
|
$lastSeenNamespace .= $token[1];
|
2012-10-22 03:58:31 +04:00
|
|
|
} elseif (is_string($token) && in_array($token, array(';', '{'))) {
|
2011-04-30 12:42:38 +04:00
|
|
|
$inNamespace = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($inClass) {
|
|
|
|
$inClass = false;
|
2011-07-13 01:43:24 +04:00
|
|
|
$lastSeenClass = $lastSeenNamespace . ($lastSeenNamespace ? '\\' : '') . $token[1];
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->staticReflection[$lastSeenClass]['properties'] = array();
|
|
|
|
$this->staticReflection[$lastSeenClass]['methods'] = array();
|
2011-04-30 12:42:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($token[0] == T_NAMESPACE) {
|
|
|
|
$lastSeenNamespace = "";
|
|
|
|
$inNamespace = true;
|
2012-10-22 03:58:31 +04:00
|
|
|
} elseif ($token[0] == T_CLASS) {
|
2011-04-30 12:42:38 +04:00
|
|
|
$inClass = true;
|
2012-10-22 03:58:31 +04:00
|
|
|
} elseif ($token[0] == T_FUNCTION) {
|
2011-02-03 01:21:42 +03:00
|
|
|
if ($tokens[$i+2][0] == T_STRING) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->staticReflection[$lastSeenClass]['methods'][] = $tokens[$i+2][1];
|
2012-10-22 03:58:31 +04:00
|
|
|
} elseif ($tokens[$i+2] == "&" && $tokens[$i+3][0] == T_STRING) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->staticReflection[$lastSeenClass]['methods'][] = $tokens[$i+3][1];
|
2011-02-03 01:21:42 +03:00
|
|
|
}
|
2012-10-22 03:58:31 +04:00
|
|
|
} elseif (in_array($token[0], array(T_VAR, T_PUBLIC, T_PRIVATE, T_PROTECTED)) && $tokens[$i+2][0] != T_FUNCTION) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->staticReflection[$lastSeenClass]['properties'][] = substr($tokens[$i+2][1], 1);
|
2011-02-03 01:21:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function hasProperty($property, ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->extendsClass()) {
|
2011-07-27 00:59:55 +04:00
|
|
|
// don't generate property if its already on the base class.
|
2012-04-01 20:29:49 +04:00
|
|
|
$reflClass = new \ReflectionClass($this->getClassToExtend());
|
2011-07-27 00:59:55 +04:00
|
|
|
if ($reflClass->hasProperty($property)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return (
|
2012-04-01 20:29:49 +04:00
|
|
|
isset($this->staticReflection[$metadata->name]) &&
|
|
|
|
in_array($property, $this->staticReflection[$metadata->name]['properties'])
|
2011-02-03 01:21:42 +03:00
|
|
|
);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function hasMethod($method, ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->extendsClass()) {
|
2011-07-27 00:59:55 +04:00
|
|
|
// don't generate method if its already on the base class.
|
2012-04-01 20:29:49 +04:00
|
|
|
$reflClass = new \ReflectionClass($this->getClassToExtend());
|
2012-10-22 03:58:31 +04:00
|
|
|
|
2011-07-27 00:59:55 +04:00
|
|
|
if ($reflClass->hasMethod($method)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return (
|
2012-04-01 20:29:49 +04:00
|
|
|
isset($this->staticReflection[$metadata->name]) &&
|
|
|
|
in_array($method, $this->staticReflection[$metadata->name]['methods'])
|
2011-02-03 01:21:42 +03:00
|
|
|
);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function hasNamespace(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
return strpos($metadata->name, '\\') ? true : false;
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function extendsClass()
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
return $this->classToExtend ? true : false;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function getClassToExtend()
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
return $this->classToExtend;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function getClassToExtendName()
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
$refl = new \ReflectionClass($this->getClassToExtend());
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-10-30 11:16:55 +04:00
|
|
|
return '\\' . $refl->getName();
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function getClassName(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2010-03-29 02:56:59 +04:00
|
|
|
return ($pos = strrpos($metadata->name, '\\'))
|
|
|
|
? substr($metadata->name, $pos + 1, strlen($metadata->name)) : $metadata->name;
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function getNamespace(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
return substr($metadata->name, 0, strrpos($metadata->name, '\\'));
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityDocBlock(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$lines = array();
|
|
|
|
$lines[] = '/**';
|
2012-11-02 20:15:44 +04:00
|
|
|
$lines[] = ' * ' . $this->getClassName($metadata);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->generateAnnotations) {
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = ' *';
|
|
|
|
|
|
|
|
$methods = array(
|
2012-04-01 20:29:49 +04:00
|
|
|
'generateTableAnnotation',
|
|
|
|
'generateInheritanceAnnotation',
|
|
|
|
'generateDiscriminatorColumnAnnotation',
|
|
|
|
'generateDiscriminatorMapAnnotation'
|
2010-03-20 01:38:45 +03:00
|
|
|
);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
foreach ($methods as $method) {
|
|
|
|
if ($code = $this->$method($metadata)) {
|
|
|
|
$lines[] = ' * ' . $code;
|
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($metadata->isMappedSuperclass) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = ' * @' . $this->annotationsPrefix . 'MappedSuperClass';
|
2010-03-20 01:38:45 +03:00
|
|
|
} else {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = ' * @' . $this->annotationsPrefix . 'Entity';
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($metadata->customRepositoryClassName) {
|
|
|
|
$lines[count($lines) - 1] .= '(repositoryClass="' . $metadata->customRepositoryClassName . '")';
|
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = ' * @' . $this->annotationsPrefix . 'HasLifecycleCallbacks';
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$lines[] = ' */';
|
|
|
|
|
|
|
|
return implode("\n", $lines);
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateTableAnnotation($metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$table = array();
|
2011-09-07 08:48:19 +04:00
|
|
|
|
|
|
|
if (isset($metadata->table['schema'])) {
|
|
|
|
$table[] = 'schema="' . $metadata->table['schema'] . '"';
|
|
|
|
}
|
2011-12-04 00:19:21 +04:00
|
|
|
|
2011-09-07 08:48:19 +04:00
|
|
|
if (isset($metadata->table['name'])) {
|
2010-03-29 17:20:41 +04:00
|
|
|
$table[] = 'name="' . $metadata->table['name'] . '"';
|
2010-03-19 01:35:16 +03:00
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-01-28 14:16:36 +04:00
|
|
|
if (isset($metadata->table['uniqueConstraints']) && $metadata->table['uniqueConstraints']) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$constraints = $this->generateTableConstraints('UniqueConstraint', $metadata->table['uniqueConstraints']);
|
2012-01-28 14:16:36 +04:00
|
|
|
$table[] = 'uniqueConstraints={' . $constraints . '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($metadata->table['indexes']) && $metadata->table['indexes']) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$constraints = $this->generateTableConstraints('Index', $metadata->table['indexes']);
|
2012-01-28 14:16:36 +04:00
|
|
|
$table[] = 'indexes={' . $constraints . '}';
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
return '@' . $this->annotationsPrefix . 'Table(' . implode(', ', $table) . ')';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateTableConstraints($constraintName, $constraints)
|
2012-01-28 14:16:36 +04:00
|
|
|
{
|
|
|
|
$annotations = array();
|
|
|
|
foreach ($constraints as $name => $constraint) {
|
|
|
|
$columns = array();
|
|
|
|
foreach ($constraint['columns'] as $column) {
|
|
|
|
$columns[] = '"' . $column . '"';
|
|
|
|
}
|
2012-04-01 20:29:49 +04:00
|
|
|
$annotations[] = '@' . $this->annotationsPrefix . $constraintName . '(name="' . $name . '", columns={' . implode(', ', $columns) . '})';
|
2012-01-28 14:16:36 +04:00
|
|
|
}
|
|
|
|
return implode(', ', $annotations);
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateInheritanceAnnotation($metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
|
2012-04-01 20:29:49 +04:00
|
|
|
return '@' . $this->annotationsPrefix . 'InheritanceType("'.$this->getInheritanceTypeString($metadata->inheritanceType).'")';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateDiscriminatorColumnAnnotation($metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
|
|
|
|
$discrColumn = $metadata->discriminatorValue;
|
|
|
|
$columnDefinition = 'name="' . $discrColumn['name']
|
|
|
|
. '", type="' . $discrColumn['type']
|
|
|
|
. '", length=' . $discrColumn['length'];
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
return '@' . $this->annotationsPrefix . 'DiscriminatorColumn(' . $columnDefinition . ')';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateDiscriminatorMapAnnotation($metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
|
|
|
|
$inheritanceClassMap = array();
|
|
|
|
|
|
|
|
foreach ($metadata->discriminatorMap as $type => $class) {
|
|
|
|
$inheritanceClassMap[] .= '"' . $type . '" = "' . $class . '"';
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
return '@' . $this->annotationsPrefix . 'DiscriminatorMap({' . implode(', ', $inheritanceClassMap) . '})';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityStubMethods(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$methods = array();
|
|
|
|
|
|
|
|
foreach ($metadata->fieldMappings as $fieldMapping) {
|
2011-02-03 01:30:16 +03:00
|
|
|
if ( ! isset($fieldMapping['id']) || ! $fieldMapping['id'] || $metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE) {
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($code = $this->generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($code = $this->generateEntityStubMethod($metadata, 'get', $fieldMapping['fieldName'], $fieldMapping['type'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($metadata->associationMappings as $associationMapping) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$nullable = $this->isAssociationIsNullable($associationMapping) ? 'null' : null;
|
|
|
|
if ($code = $this->generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($code = $this->generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
2012-10-22 03:58:31 +04:00
|
|
|
} elseif ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($code = $this->generateEntityStubMethod($metadata, 'add', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
2012-07-04 23:03:50 +04:00
|
|
|
if ($code = $this->generateEntityStubMethod($metadata, 'remove', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
|
|
|
|
$methods[] = $code;
|
|
|
|
}
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($code = $this->generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], 'Doctrine\Common\Collections\Collection')) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
return implode("\n\n", $methods);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function isAssociationIsNullable($associationMapping)
|
2011-10-21 11:38:37 +04:00
|
|
|
{
|
2012-02-15 15:43:55 +04:00
|
|
|
if (isset($associationMapping['id']) && $associationMapping['id']) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-22 03:58:31 +04:00
|
|
|
|
2011-10-24 12:19:01 +04:00
|
|
|
if (isset($associationMapping['joinColumns'])) {
|
2011-10-21 11:38:37 +04:00
|
|
|
$joinColumns = $associationMapping['joinColumns'];
|
2011-10-24 12:19:01 +04:00
|
|
|
} else {
|
2012-10-22 03:58:31 +04:00
|
|
|
//@todo there is no way to retrieve targetEntity metadata
|
2011-10-21 11:38:37 +04:00
|
|
|
$joinColumns = array();
|
|
|
|
}
|
2012-10-22 03:58:31 +04:00
|
|
|
|
2011-10-21 11:38:37 +04:00
|
|
|
foreach ($joinColumns as $joinColumn) {
|
2011-10-24 12:19:01 +04:00
|
|
|
if(isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
|
2011-10-21 11:38:37 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-10-22 03:58:31 +04:00
|
|
|
|
2011-10-21 11:38:37 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
|
|
|
|
$methods = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
foreach ($metadata->lifecycleCallbacks as $name => $callbacks) {
|
|
|
|
foreach ($callbacks as $callback) {
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($code = $this->generateLifecycleCallbackMethod($name, $callback, $metadata)) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return implode("\n\n", $methods);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2011-02-03 01:21:42 +03:00
|
|
|
|
|
|
|
return "";
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$lines = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
foreach ($metadata->associationMappings as $associationMapping) {
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->hasProperty($associationMapping['fieldName'], $metadata)) {
|
2010-03-19 00:38:42 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->generateAssociationMappingPropertyDocBlock($associationMapping, $metadata);
|
|
|
|
$lines[] = $this->spaces . $this->fieldVisibility . ' $' . $associationMapping['fieldName']
|
2010-08-09 15:13:21 +04:00
|
|
|
. ($associationMapping['type'] == 'manyToMany' ? ' = array()' : null) . ";\n";
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
return implode("\n", $lines);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityFieldMappingProperties(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$lines = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
foreach ($metadata->fieldMappings as $fieldMapping) {
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->hasProperty($fieldMapping['fieldName'], $metadata) ||
|
2011-02-12 19:40:07 +03:00
|
|
|
$metadata->isInheritedField($fieldMapping['fieldName'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->generateFieldMappingPropertyDocBlock($fieldMapping, $metadata);
|
|
|
|
$lines[] = $this->spaces . $this->fieldVisibility . ' $' . $fieldMapping['fieldName']
|
2010-03-29 02:56:59 +04:00
|
|
|
. (isset($fieldMapping['default']) ? ' = ' . var_export($fieldMapping['default'], true) : null) . ";\n";
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
|
|
|
return implode("\n", $lines);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-05-27 22:31:01 +04:00
|
|
|
$methodName = $type . Inflector::classify($fieldName);
|
2012-07-04 23:03:50 +04:00
|
|
|
if (in_array($type, array("add", "remove")) && substr($methodName, -1) == "s") {
|
2012-05-27 22:31:01 +04:00
|
|
|
$methodName = substr($methodName, 0, -1);
|
2011-07-13 01:25:15 +04:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->hasMethod($methodName, $metadata)) {
|
2012-10-22 03:58:31 +04:00
|
|
|
return '';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->staticReflection[$metadata->name]['methods'][] = $methodName;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$var = sprintf('%sMethodTemplate', $type);
|
2010-03-20 01:38:45 +03:00
|
|
|
$template = self::$$var;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-11-06 03:53:07 +04:00
|
|
|
$methodTypeHint = null;
|
2012-04-01 20:29:49 +04:00
|
|
|
$types = Type::getTypesMap();
|
|
|
|
$variableType = $typeHint ? $this->getType($typeHint) . ' ' : null;
|
2012-11-06 03:53:07 +04:00
|
|
|
|
|
|
|
if ($typeHint && ! isset($types[$typeHint])) {
|
|
|
|
$variableType = '\\' . ltrim($variableType, '\\');
|
|
|
|
$methodTypeHint = '\\' . $typeHint . ' ';
|
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$replacements = array(
|
|
|
|
'<description>' => ucfirst($type) . ' ' . $fieldName,
|
|
|
|
'<methodTypeHint>' => $methodTypeHint,
|
|
|
|
'<variableType>' => $variableType,
|
|
|
|
'<variableName>' => Inflector::camelize($fieldName),
|
|
|
|
'<methodName>' => $methodName,
|
2011-09-20 16:59:32 +04:00
|
|
|
'<fieldName>' => $fieldName,
|
2011-12-04 00:19:21 +04:00
|
|
|
'<variableDefault>' => ($defaultValue !== null ) ? (' = '.$defaultValue) : '',
|
2012-04-01 20:29:49 +04:00
|
|
|
'<entity>' => $this->getClassName($metadata)
|
2010-03-20 01:38:45 +03:00
|
|
|
);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$method = str_replace(
|
|
|
|
array_keys($replacements),
|
|
|
|
array_values($replacements),
|
|
|
|
$template
|
|
|
|
);
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
return $this->prefixCodeWithSpaces($method);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateLifecycleCallbackMethod($name, $methodName, $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->hasMethod($methodName, $metadata)) {
|
2012-10-22 03:58:31 +04:00
|
|
|
return '';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2012-04-01 20:29:49 +04:00
|
|
|
$this->staticReflection[$metadata->name]['methods'][] = $methodName;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$replacements = array(
|
2012-04-01 20:29:49 +04:00
|
|
|
'<name>' => $this->annotationsPrefix . ucfirst($name),
|
2010-03-20 01:38:45 +03:00
|
|
|
'<methodName>' => $methodName,
|
|
|
|
);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$method = str_replace(
|
|
|
|
array_keys($replacements),
|
|
|
|
array_values($replacements),
|
2012-04-01 20:29:49 +04:00
|
|
|
self::$lifecycleCallbackMethodTemplate
|
2010-03-20 01:38:45 +03:00
|
|
|
);
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
return $this->prefixCodeWithSpaces($method);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateJoinColumnAnnotation(array $joinColumn)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$joinColumnAnnot = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if (isset($joinColumn['name'])) {
|
|
|
|
$joinColumnAnnot[] = 'name="' . $joinColumn['name'] . '"';
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if (isset($joinColumn['referencedColumnName'])) {
|
|
|
|
$joinColumnAnnot[] = 'referencedColumnName="' . $joinColumn['referencedColumnName'] . '"';
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if (isset($joinColumn['unique']) && $joinColumn['unique']) {
|
|
|
|
$joinColumnAnnot[] = 'unique=' . ($joinColumn['unique'] ? 'true' : 'false');
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if (isset($joinColumn['nullable'])) {
|
|
|
|
$joinColumnAnnot[] = 'nullable=' . ($joinColumn['nullable'] ? 'true' : 'false');
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if (isset($joinColumn['onDelete'])) {
|
2011-10-15 19:53:04 +04:00
|
|
|
$joinColumnAnnot[] = 'onDelete="' . ($joinColumn['onDelete'] . '"');
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if (isset($joinColumn['columnDefinition'])) {
|
|
|
|
$joinColumnAnnot[] = 'columnDefinition="' . $joinColumn['columnDefinition'] . '"';
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
return '@' . $this->annotationsPrefix . 'JoinColumn(' . implode(', ', $joinColumnAnnot) . ')';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$lines = array();
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . '/**';
|
2011-10-24 12:19:01 +04:00
|
|
|
|
2011-10-19 11:25:40 +04:00
|
|
|
if ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
|
2012-11-06 03:53:07 +04:00
|
|
|
$lines[] = $this->spaces . ' * @var \Doctrine\Common\Collections\Collection';
|
2011-10-24 12:19:01 +04:00
|
|
|
} else {
|
2012-11-06 03:53:07 +04:00
|
|
|
$lines[] = $this->spaces . ' * @var \\' . ltrim($associationMapping['targetEntity'], '\\');
|
2011-10-19 11:25:40 +04:00
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->generateAnnotations) {
|
|
|
|
$lines[] = $this->spaces . ' *';
|
2012-06-06 10:44:25 +04:00
|
|
|
|
2012-02-13 13:35:55 +04:00
|
|
|
if (isset($associationMapping['id']) && $associationMapping['id']) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'Id';
|
2012-06-06 10:44:25 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($generatorType = $this->getIdGeneratorTypeString($metadata->generatorType)) {
|
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'GeneratedValue(strategy="' . $generatorType . '")';
|
2012-02-13 13:35:55 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$type = null;
|
|
|
|
switch ($associationMapping['type']) {
|
|
|
|
case ClassMetadataInfo::ONE_TO_ONE:
|
|
|
|
$type = 'OneToOne';
|
|
|
|
break;
|
|
|
|
case ClassMetadataInfo::MANY_TO_ONE:
|
|
|
|
$type = 'ManyToOne';
|
|
|
|
break;
|
|
|
|
case ClassMetadataInfo::ONE_TO_MANY:
|
|
|
|
$type = 'OneToMany';
|
|
|
|
break;
|
|
|
|
case ClassMetadataInfo::MANY_TO_MANY:
|
|
|
|
$type = 'ManyToMany';
|
|
|
|
break;
|
|
|
|
}
|
2010-03-20 01:38:45 +03:00
|
|
|
$typeOptions = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['targetEntity'])) {
|
|
|
|
$typeOptions[] = 'targetEntity="' . $associationMapping['targetEntity'] . '"';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['inversedBy'])) {
|
|
|
|
$typeOptions[] = 'inversedBy="' . $associationMapping['inversedBy'] . '"';
|
2010-06-16 19:47:22 +04:00
|
|
|
}
|
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['mappedBy'])) {
|
|
|
|
$typeOptions[] = 'mappedBy="' . $associationMapping['mappedBy'] . '"';
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($associationMapping['cascade']) {
|
2010-03-20 01:38:45 +03:00
|
|
|
$cascades = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($associationMapping['isCascadePersist']) $cascades[] = '"persist"';
|
|
|
|
if ($associationMapping['isCascadeRemove']) $cascades[] = '"remove"';
|
|
|
|
if ($associationMapping['isCascadeDetach']) $cascades[] = '"detach"';
|
|
|
|
if ($associationMapping['isCascadeMerge']) $cascades[] = '"merge"';
|
|
|
|
if ($associationMapping['isCascadeRefresh']) $cascades[] = '"refresh"';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-09-30 02:12:06 +04:00
|
|
|
if (count($cascades) === 5) {
|
|
|
|
$cascades = array('"all"');
|
|
|
|
}
|
2012-10-02 01:16:29 +04:00
|
|
|
|
2011-05-25 14:35:54 +04:00
|
|
|
$typeOptions[] = 'cascade={' . implode(',', $cascades) . '}';
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['orphanRemoval']) && $associationMapping['orphanRemoval']) {
|
|
|
|
$typeOptions[] = 'orphanRemoval=' . ($associationMapping['orphanRemoval'] ? 'true' : 'false');
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . '' . $type . '(' . implode(', ', $typeOptions) . ')';
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['joinColumns']) && $associationMapping['joinColumns']) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'JoinColumns({';
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$joinColumnsLines = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($associationMapping['joinColumns'] as $joinColumn) {
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($joinColumnAnnot = $this->generateJoinColumnAnnotation($joinColumn)) {
|
|
|
|
$joinColumnsLines[] = $this->spaces . ' * ' . $joinColumnAnnot;
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = implode(",\n", $joinColumnsLines);
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * })';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['joinTable']) && $associationMapping['joinTable']) {
|
2010-03-20 01:38:45 +03:00
|
|
|
$joinTable = array();
|
2010-08-09 15:13:21 +04:00
|
|
|
$joinTable[] = 'name="' . $associationMapping['joinTable']['name'] . '"';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['joinTable']['schema'])) {
|
|
|
|
$joinTable[] = 'schema="' . $associationMapping['joinTable']['schema'] . '"';
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'JoinTable(' . implode(', ', $joinTable) . ',';
|
|
|
|
$lines[] = $this->spaces . ' * joinColumns={';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-10-20 22:37:13 +04:00
|
|
|
$joinColumnsLines = array();
|
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($associationMapping['joinTable']['joinColumns'] as $joinColumn) {
|
2012-10-20 22:37:13 +04:00
|
|
|
$joinColumnsLines[] = $this->spaces . ' * ' . $this->generateJoinColumnAnnotation($joinColumn);
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
2012-10-20 22:37:13 +04:00
|
|
|
$lines[] = implode(",". PHP_EOL, $joinColumnsLines);
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * },';
|
|
|
|
$lines[] = $this->spaces . ' * inverseJoinColumns={';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-10-20 22:37:13 +04:00
|
|
|
$inverseJoinColumnsLines = array();
|
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($associationMapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
|
2012-10-20 22:37:13 +04:00
|
|
|
$inverseJoinColumnsLines[] = $this->spaces . ' * ' . $this->generateJoinColumnAnnotation($joinColumn);
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
|
|
|
|
2012-10-20 22:37:13 +04:00
|
|
|
$lines[] = implode(",". PHP_EOL, $inverseJoinColumnsLines);
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * }';
|
|
|
|
$lines[] = $this->spaces . ' * )';
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['orderBy'])) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'OrderBy({';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($associationMapping['orderBy'] as $name => $direction) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * "' . $name . '"="' . $direction . '",';
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[count($lines) - 1] = substr($lines[count($lines) - 1], 0, strlen($lines[count($lines) - 1]) - 1);
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * })';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' */';
|
2010-03-19 00:38:42 +03:00
|
|
|
|
|
|
|
return implode("\n", $lines);
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$lines = array();
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . '/**';
|
2012-11-02 20:15:44 +04:00
|
|
|
$lines[] = $this->spaces . ' * @var ' . $this->getType($fieldMapping['type']);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($this->generateAnnotations) {
|
|
|
|
$lines[] = $this->spaces . ' *';
|
2010-03-20 01:38:45 +03:00
|
|
|
|
|
|
|
$column = array();
|
|
|
|
if (isset($fieldMapping['columnName'])) {
|
|
|
|
$column[] = 'name="' . $fieldMapping['columnName'] . '"';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['type'])) {
|
|
|
|
$column[] = 'type="' . $fieldMapping['type'] . '"';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['length'])) {
|
|
|
|
$column[] = 'length=' . $fieldMapping['length'];
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['precision'])) {
|
|
|
|
$column[] = 'precision=' . $fieldMapping['precision'];
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['scale'])) {
|
|
|
|
$column[] = 'scale=' . $fieldMapping['scale'];
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['nullable'])) {
|
|
|
|
$column[] = 'nullable=' . var_export($fieldMapping['nullable'], true);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['columnDefinition'])) {
|
|
|
|
$column[] = 'columnDefinition="' . $fieldMapping['columnDefinition'] . '"';
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['unique'])) {
|
|
|
|
$column[] = 'unique=' . var_export($fieldMapping['unique'], true);
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'Column(' . implode(', ', $column) . ')';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['id']) && $fieldMapping['id']) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'Id';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
if ($generatorType = $this->getIdGeneratorTypeString($metadata->generatorType)) {
|
|
|
|
$lines[] = $this->spaces.' * @' . $this->annotationsPrefix . 'GeneratedValue(strategy="' . $generatorType . '")';
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($metadata->sequenceGeneratorDefinition) {
|
|
|
|
$sequenceGenerator = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($metadata->sequenceGeneratorDefinition['sequenceName'])) {
|
|
|
|
$sequenceGenerator[] = 'sequenceName="' . $metadata->sequenceGeneratorDefinition['sequenceName'] . '"';
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($metadata->sequenceGeneratorDefinition['allocationSize'])) {
|
2012-04-19 03:39:21 +04:00
|
|
|
$sequenceGenerator[] = 'allocationSize=' . $metadata->sequenceGeneratorDefinition['allocationSize'];
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($metadata->sequenceGeneratorDefinition['initialValue'])) {
|
2012-04-19 03:39:21 +04:00
|
|
|
$sequenceGenerator[] = 'initialValue=' . $metadata->sequenceGeneratorDefinition['initialValue'];
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'SequenceGenerator(' . implode(', ', $sequenceGenerator) . ')';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'Version';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
2010-03-20 01:38:45 +03:00
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
$lines[] = $this->spaces . ' */';
|
2010-03-19 00:38:42 +03:00
|
|
|
|
|
|
|
return implode("\n", $lines);
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function prefixCodeWithSpaces($code, $num = 1)
|
2010-03-20 01:38:45 +03:00
|
|
|
{
|
|
|
|
$lines = explode("\n", $code);
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
foreach ($lines as $key => $value) {
|
2012-11-04 19:22:32 +04:00
|
|
|
if ( ! empty($value)) {
|
2012-10-31 02:22:04 +04:00
|
|
|
$lines[$key] = str_repeat($this->spaces, $num) . $lines[$key];
|
|
|
|
}
|
2010-03-20 01:38:45 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
return implode("\n", $lines);
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function getInheritanceTypeString($type)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2010-03-29 02:56:59 +04:00
|
|
|
switch ($type) {
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::INHERITANCE_TYPE_NONE:
|
|
|
|
return 'NONE';
|
|
|
|
|
|
|
|
case ClassMetadataInfo::INHERITANCE_TYPE_JOINED:
|
|
|
|
return 'JOINED';
|
2010-03-29 02:29:29 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE:
|
|
|
|
return 'SINGLE_TABLE';
|
2010-03-29 02:29:29 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS:
|
|
|
|
return 'PER_CLASS';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException('Invalid provided InheritanceType: ' . $type);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function getChangeTrackingPolicyString($policy)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2010-03-29 02:56:59 +04:00
|
|
|
switch ($policy) {
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT:
|
|
|
|
return 'DEFERRED_IMPLICIT';
|
2010-03-29 02:29:29 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT:
|
|
|
|
return 'DEFERRED_EXPLICIT';
|
2010-03-29 02:29:29 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::CHANGETRACKING_NOTIFY:
|
|
|
|
return 'NOTIFY';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException('Invalid provided ChangeTrackingPolicy: ' . $policy);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 20:29:49 +04:00
|
|
|
private function getIdGeneratorTypeString($type)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2010-03-29 02:56:59 +04:00
|
|
|
switch ($type) {
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::GENERATOR_TYPE_AUTO:
|
|
|
|
return 'AUTO';
|
2010-03-29 02:29:29 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE:
|
|
|
|
return 'SEQUENCE';
|
2010-03-29 02:29:29 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::GENERATOR_TYPE_TABLE:
|
|
|
|
return 'TABLE';
|
2010-03-29 02:29:29 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
case ClassMetadataInfo::GENERATOR_TYPE_IDENTITY:
|
|
|
|
return 'IDENTITY';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-04-13 04:54:43 +04:00
|
|
|
case ClassMetadataInfo::GENERATOR_TYPE_NONE:
|
|
|
|
return 'NONE';
|
|
|
|
|
2010-03-29 02:56:59 +04:00
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException('Invalid provided IdGeneratorType: ' . $type);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
2012-01-12 14:20:49 +04:00
|
|
|
}
|