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
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo,
|
2010-03-20 01:38:45 +03:00
|
|
|
Doctrine\ORM\Mapping\AssociationMapping,
|
|
|
|
Doctrine\Common\Util\Inflector;
|
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');
|
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @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
|
|
|
|
{
|
2011-02-03 01:21:42 +03:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $_backupExisting = true;
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
/** The extension to use for written php files */
|
|
|
|
private $_extension = '.php';
|
|
|
|
|
|
|
|
/** Whether or not the current ClassMetadataInfo instance is new or old */
|
2010-03-19 07:19:15 +03:00
|
|
|
private $_isNew = true;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
private $_staticReflection = array();
|
2010-03-19 00:38:42 +03:00
|
|
|
|
|
|
|
/** Number of spaces to use for indention in generated code */
|
|
|
|
private $_numSpaces = 4;
|
|
|
|
|
|
|
|
/** The actual spaces to use for indention */
|
|
|
|
private $_spaces = ' ';
|
|
|
|
|
|
|
|
/** The class all generated entities should extend */
|
|
|
|
private $_classToExtend;
|
|
|
|
|
|
|
|
/** Whether or not to generation annotations */
|
|
|
|
private $_generateAnnotations = false;
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $_annotationsPrefix = '';
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
/** Whether or not to generated sub methods */
|
2010-03-19 07:19:15 +03:00
|
|
|
private $_generateEntityStubMethods = false;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
|
|
|
/** Whether or not to update the entity class if it exists already */
|
|
|
|
private $_updateEntityIfExists = false;
|
|
|
|
|
|
|
|
/** Whether or not to re-generate entity class if it exists already */
|
|
|
|
private $_regenerateEntityIfExists = false;
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
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>
|
|
|
|
}';
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private static $_getMethodTemplate =
|
|
|
|
'/**
|
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
|
|
|
}';
|
|
|
|
|
|
|
|
private static $_setMethodTemplate =
|
|
|
|
'/**
|
2010-03-20 01:47:34 +03:00
|
|
|
* <description>
|
|
|
|
*
|
|
|
|
* @param <variableType>$<variableName>
|
|
|
|
*/
|
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>;
|
2010-03-20 01:38:45 +03:00
|
|
|
}';
|
|
|
|
|
|
|
|
private static $_addMethodTemplate =
|
|
|
|
'/**
|
2010-03-20 01:47:34 +03:00
|
|
|
* <description>
|
|
|
|
*
|
|
|
|
* @param <variableType>$<variableName>
|
|
|
|
*/
|
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>;
|
2010-03-20 01:38:45 +03:00
|
|
|
}';
|
|
|
|
|
|
|
|
private static $_lifecycleCallbackMethodTemplate =
|
|
|
|
'/**
|
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
|
|
|
}';
|
|
|
|
|
2011-02-03 01:30:16 +03:00
|
|
|
private static $_constructorMethodTemplate =
|
|
|
|
'public function __construct()
|
|
|
|
{
|
|
|
|
<spaces><collections>
|
|
|
|
}
|
|
|
|
';
|
|
|
|
|
2011-05-25 14:35:54 +04:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (version_compare(\Doctrine\Common\Version::VERSION, '3.0.0-DEV', '>=')) {
|
|
|
|
$this->_annotationsPrefix = 'ORM\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension;
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->_isNew = !file_exists($path) || (file_exists($path) && $this->_regenerateEntityIfExists);
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-19 01:35:16 +03:00
|
|
|
if ( ! $this->_isNew) {
|
2011-04-30 12:42:38 +04:00
|
|
|
$this->_parseTokensInEntityFile(file_get_contents($path));
|
2011-02-03 01:21:42 +03:00
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2011-02-03 01:21:42 +03: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)) {
|
|
|
|
throw new \RuntimeException("Attempt to backup overwritten entitiy file but copy operation failed.");
|
|
|
|
}
|
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
|
2011-02-03 01:21:42 +03: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
|
|
|
|
} else if ( ! $this->_isNew && $this->_updateEntityIfExists) {
|
|
|
|
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(
|
|
|
|
$this->_generateEntityNamespace($metadata),
|
2010-03-20 01:38:45 +03:00
|
|
|
$this->_generateEntityDocBlock($metadata),
|
2010-03-19 00:38:42 +03:00
|
|
|
$this->_generateEntityClassName($metadata),
|
2010-04-14 22:17:55 +04:00
|
|
|
$this->_generateEntityBody($metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
);
|
|
|
|
|
2010-04-14 22:17:55 +04:00
|
|
|
$code = str_replace($placeHolders, $replacements, self::$_classTemplate);
|
|
|
|
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
|
|
|
|
|
|
|
$body = $this->_generateEntityBody($metadata);
|
2010-04-14 22:17:55 +04:00
|
|
|
$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
|
|
|
|
2010-11-27 21:57:37 +03:00
|
|
|
return substr($currentCode, 0, $last) . $body . (strlen($body) > 0 ? "\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)
|
|
|
|
{
|
|
|
|
$this->_spaces = str_repeat(' ', $numSpaces);
|
|
|
|
$this->_numSpaces = $numSpaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
$this->_extension = $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the name of the class the generated classes should extend from
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setClassToExtend($classToExtend)
|
|
|
|
{
|
|
|
|
$this->_classToExtend = $classToExtend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
$this->_generateAnnotations = $bool;
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
/**
|
|
|
|
* Set an annotation prefix.
|
|
|
|
*
|
|
|
|
* @param string $prefix
|
|
|
|
*/
|
|
|
|
public function setAnnotationPrefix($prefix)
|
|
|
|
{
|
2011-08-13 23:28:05 +04:00
|
|
|
if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
|
2011-05-25 14:35:54 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->_annotationsPrefix = $prefix;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$this->_updateEntityIfExists = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether or not to regenerate the entity if it exists
|
|
|
|
*
|
|
|
|
* @param bool $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setRegenerateEntityIfExists($bool)
|
|
|
|
{
|
|
|
|
$this->_regenerateEntityIfExists = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether or not to generate stub methods for the entity
|
|
|
|
*
|
|
|
|
* @param bool $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setGenerateStubMethods($bool)
|
|
|
|
{
|
2010-03-19 07:19:15 +03: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)
|
|
|
|
{
|
|
|
|
$this->_backupExisting = $bool;
|
|
|
|
}
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
private function _generateEntityNamespace(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
if ($this->_hasNamespace($metadata)) {
|
|
|
|
return 'namespace ' . $this->_getNamespace($metadata) .';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateEntityClassName(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
return 'class ' . $this->_getClassName($metadata) .
|
2010-03-28 21:46:23 +04:00
|
|
|
($this->_extendsClass() ? ' extends ' . $this->_getClassToExtendName() : null);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateEntityBody(ClassMetadataInfo $metadata)
|
|
|
|
{
|
2011-02-03 01:30:16 +03:00
|
|
|
$fieldMappingProperties = $this->_generateEntityFieldMappingProperties($metadata);
|
2010-03-19 00:38:42 +03:00
|
|
|
$associationMappingProperties = $this->_generateEntityAssociationMappingProperties($metadata);
|
2010-03-19 07:19:15 +03:00
|
|
|
$stubMethods = $this->_generateEntityStubMethods ? $this->_generateEntityStubMethods($metadata) : null;
|
2010-03-19 00:38:42 +03:00
|
|
|
$lifecycleCallbackMethods = $this->_generateEntityLifecycleCallbackMethods($metadata);
|
|
|
|
|
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
|
|
|
|
2011-02-03 01:30:16 +03:00
|
|
|
$code[] = $this->_generateEntityConstructor($metadata);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-02-03 01:30:16 +03:00
|
|
|
private function _generateEntityConstructor(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
if ($this->_hasMethod('__construct', $metadata)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$collections = array();
|
2011-10-03 09:07:07 +04:00
|
|
|
|
2011-02-03 01:30:16 +03:00
|
|
|
foreach ($metadata->associationMappings AS $mapping) {
|
|
|
|
if ($mapping['type'] & ClassMetadataInfo::TO_MANY) {
|
|
|
|
$collections[] = '$this->'.$mapping['fieldName'].' = new \Doctrine\Common\Collections\ArrayCollection();';
|
|
|
|
}
|
|
|
|
}
|
2011-10-03 09:07:07 +04:00
|
|
|
|
2011-02-03 01:30:16 +03:00
|
|
|
if ($collections) {
|
|
|
|
return $this->_prefixCodeWithSpaces(str_replace("<collections>", implode("\n", $collections), self::$_constructorMethodTemplate));
|
|
|
|
}
|
2011-10-03 09:07:07 +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
|
|
|
*/
|
2011-04-30 12:42:38 +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;
|
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];
|
|
|
|
} else if (is_string($token) && in_array($token, array(';', '{'))) {
|
|
|
|
$inNamespace = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($inClass) {
|
|
|
|
$inClass = false;
|
2011-07-13 01:43:24 +04:00
|
|
|
$lastSeenClass = $lastSeenNamespace . ($lastSeenNamespace ? '\\' : '') . $token[1];
|
2011-02-03 01:21:42 +03: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;
|
|
|
|
} else if ($token[0] == T_CLASS) {
|
|
|
|
$inClass = true;
|
2011-02-03 01:21:42 +03:00
|
|
|
} else if ($token[0] == T_FUNCTION) {
|
|
|
|
if ($tokens[$i+2][0] == T_STRING) {
|
|
|
|
$this->_staticReflection[$lastSeenClass]['methods'][] = $tokens[$i+2][1];
|
2011-07-09 16:53:25 +04:00
|
|
|
} else if ($tokens[$i+2] == "&" && $tokens[$i+3][0] == T_STRING) {
|
2011-02-03 01:21:42 +03:00
|
|
|
$this->_staticReflection[$lastSeenClass]['methods'][] = $tokens[$i+3][1];
|
|
|
|
}
|
|
|
|
} else if (in_array($token[0], array(T_VAR, T_PUBLIC, T_PRIVATE, T_PROTECTED)) && $tokens[$i+2][0] != T_FUNCTION) {
|
|
|
|
$this->_staticReflection[$lastSeenClass]['properties'][] = substr($tokens[$i+2][1], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private function _hasProperty($property, ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2011-07-27 00:59:55 +04:00
|
|
|
if ($this->_extendsClass()) {
|
|
|
|
// don't generate property if its already on the base class.
|
|
|
|
$reflClass = new \ReflectionClass($this->_getClassToExtend());
|
|
|
|
if ($reflClass->hasProperty($property)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return (
|
|
|
|
isset($this->_staticReflection[$metadata->name]) &&
|
|
|
|
in_array($property, $this->_staticReflection[$metadata->name]['properties'])
|
|
|
|
);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private function _hasMethod($method, ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2011-07-27 00:59:55 +04:00
|
|
|
if ($this->_extendsClass()) {
|
|
|
|
// don't generate method if its already on the base class.
|
|
|
|
$reflClass = new \ReflectionClass($this->_getClassToExtend());
|
|
|
|
if ($reflClass->hasMethod($method)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return (
|
|
|
|
isset($this->_staticReflection[$metadata->name]) &&
|
|
|
|
in_array($method, $this->_staticReflection[$metadata->name]['methods'])
|
|
|
|
);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private function _hasNamespace(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
return strpos($metadata->name, '\\') ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _extendsClass()
|
|
|
|
{
|
|
|
|
return $this->_classToExtend ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getClassToExtend()
|
|
|
|
{
|
|
|
|
return $this->_classToExtend;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getClassToExtendName()
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03: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
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private function _getNamespace(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
return substr($metadata->name, 0, strrpos($metadata->name, '\\'));
|
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private function _generateEntityDocBlock(ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$lines = array();
|
|
|
|
$lines[] = '/**';
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = ' * '.$metadata->name;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($this->_generateAnnotations) {
|
|
|
|
$lines[] = ' *';
|
|
|
|
|
|
|
|
$methods = array(
|
|
|
|
'_generateTableAnnotation',
|
|
|
|
'_generateInheritanceAnnotation',
|
|
|
|
'_generateDiscriminatorColumnAnnotation',
|
|
|
|
'_generateDiscriminatorMapAnnotation'
|
|
|
|
);
|
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) {
|
2011-05-01 12:01:38 +04:00
|
|
|
$lines[] = ' * @' . $this->_annotationsPrefix . 'MappedSuperClass';
|
2010-03-20 01:38:45 +03:00
|
|
|
} else {
|
2011-02-03 01:21:42 +03: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) {
|
2011-02-03 01:21:42 +03: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateTableAnnotation($metadata)
|
|
|
|
{
|
|
|
|
$table = array();
|
2011-09-07 08:48:19 +04:00
|
|
|
|
|
|
|
if (isset($metadata->table['schema'])) {
|
|
|
|
$table[] = 'schema="' . $metadata->table['schema'] . '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return '@' . $this->_annotationsPrefix . 'Table(' . implode(', ', $table) . ')';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateInheritanceAnnotation($metadata)
|
|
|
|
{
|
|
|
|
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
|
2011-02-03 01:21:42 +03:00
|
|
|
return '@' . $this->_annotationsPrefix . 'InheritanceType("'.$this->_getInheritanceTypeString($metadata->inheritanceType).'")';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateDiscriminatorColumnAnnotation($metadata)
|
|
|
|
{
|
|
|
|
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
|
|
|
|
$discrColumn = $metadata->discriminatorValue;
|
|
|
|
$columnDefinition = 'name="' . $discrColumn['name']
|
|
|
|
. '", type="' . $discrColumn['type']
|
|
|
|
. '", length=' . $discrColumn['length'];
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return '@' . $this->_annotationsPrefix . 'DiscriminatorColumn(' . $columnDefinition . ')';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateDiscriminatorMapAnnotation($metadata)
|
|
|
|
{
|
|
|
|
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
|
|
|
|
$inheritanceClassMap = array();
|
|
|
|
|
|
|
|
foreach ($metadata->discriminatorMap as $type => $class) {
|
|
|
|
$inheritanceClassMap[] .= '"' . $type . '" = "' . $class . '"';
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return '@' . $this->_annotationsPrefix . 'DiscriminatorMap({' . implode(', ', $inheritanceClassMap) . '})';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateEntityStubMethods(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
$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) {
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($code = $this->_generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03: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) {
|
|
|
|
if ($code = $this->_generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($code = $this->_generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
2011-02-03 01:21:42 +03:00
|
|
|
} else if ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($code = $this->_generateEntityStubMethod($metadata, 'add', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
$methods[] = $code;
|
|
|
|
}
|
2010-08-09 15:13:21 +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
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
if ($code = $this->_generateLifecycleCallbackMethod($name, $callback, $metadata)) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
$lines = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
foreach ($metadata->associationMappings as $associationMapping) {
|
2010-08-09 15:13:21 +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
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = $this->_generateAssociationMappingPropertyDocBlock($associationMapping, $metadata);
|
2010-08-09 15:13:21 +04:00
|
|
|
$lines[] = $this->_spaces . 'private $' . $associationMapping['fieldName']
|
|
|
|
. ($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
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateEntityFieldMappingProperties(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
$lines = array();
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
foreach ($metadata->fieldMappings as $fieldMapping) {
|
2011-02-12 19:40:07 +03:00
|
|
|
if ($this->_hasProperty($fieldMapping['fieldName'], $metadata) ||
|
|
|
|
$metadata->isInheritedField($fieldMapping['fieldName'])) {
|
2010-03-19 00:38:42 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = $this->_generateFieldMappingPropertyDocBlock($fieldMapping, $metadata);
|
2010-03-29 02:56:59 +04:00
|
|
|
$lines[] = $this->_spaces . 'private $' . $fieldMapping['fieldName']
|
|
|
|
. (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
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private function _generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
2011-07-13 01:25:15 +04:00
|
|
|
if ($type == "add") {
|
|
|
|
$addMethod = explode("\\", $typeHint);
|
|
|
|
$addMethod = end($addMethod);
|
|
|
|
$methodName = $type . $addMethod;
|
|
|
|
} else {
|
|
|
|
$methodName = $type . Inflector::classify($fieldName);
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
if ($this->_hasMethod($methodName, $metadata)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-07-09 17:11:16 +04:00
|
|
|
$this->_staticReflection[$metadata->name]['methods'][] = $methodName;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$var = sprintf('_%sMethodTemplate', $type);
|
|
|
|
$template = self::$$var;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$variableType = $typeHint ? $typeHint . ' ' : null;
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$types = \Doctrine\DBAL\Types\Type::getTypesMap();
|
2010-03-29 02:29:29 +04:00
|
|
|
$methodTypeHint = $typeHint && ! isset($types[$typeHint]) ? '\\' . $typeHint . ' ' : null;
|
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,
|
|
|
|
'<fieldName>' => $fieldName
|
|
|
|
);
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->_prefixCodeWithSpaces($method);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateLifecycleCallbackMethod($name, $methodName, $metadata)
|
|
|
|
{
|
|
|
|
if ($this->_hasMethod($methodName, $metadata)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-07-09 17:11:16 +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(
|
2011-02-03 01:21:42 +03:00
|
|
|
'<name>' => $this->_annotationsPrefix . $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),
|
|
|
|
self::$_lifecycleCallbackMethodTemplate
|
|
|
|
);
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
return $this->_prefixCodeWithSpaces($method);
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _generateJoinColumnAnnotation(array $joinColumn)
|
|
|
|
{
|
|
|
|
$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'])) {
|
|
|
|
$joinColumnAnnot[] = 'onDelete=' . ($joinColumn['onDelete'] ? 'true' : 'false');
|
|
|
|
}
|
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
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
return '@' . $this->_annotationsPrefix . 'JoinColumn(' . implode(', ', $joinColumnAnnot) . ')';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
private function _generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$lines = array();
|
|
|
|
$lines[] = $this->_spaces . '/**';
|
2010-08-09 15:13:21 +04:00
|
|
|
$lines[] = $this->_spaces . ' * @var ' . $associationMapping['targetEntity'];
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($this->_generateAnnotations) {
|
|
|
|
$lines[] = $this->_spaces . ' *';
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03: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']) {
|
2011-02-03 01:21:42 +03: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) {
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($joinColumnAnnot = $this->_generateJoinColumnAnnotation($joinColumn)) {
|
|
|
|
$joinColumnsLines[] = $this->_spaces . ' * ' . $joinColumnAnnot;
|
|
|
|
}
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = implode(",\n", $joinColumnsLines);
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2011-02-03 01:21:42 +03:00
|
|
|
$lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'JoinTable(' . implode(', ', $joinTable) . ',';
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = $this->_spaces . ' * joinColumns={';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($associationMapping['joinTable']['joinColumns'] as $joinColumn) {
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = $this->_spaces . ' * ' . $this->_generateJoinColumnAnnotation($joinColumn);
|
|
|
|
}
|
|
|
|
|
2010-03-29 02:56:59 +04:00
|
|
|
$lines[] = $this->_spaces . ' * },';
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = $this->_spaces . ' * inverseJoinColumns={';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
foreach ($associationMapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = $this->_spaces . ' * ' . $this->_generateJoinColumnAnnotation($joinColumn);
|
|
|
|
}
|
|
|
|
|
2010-03-29 02:56:59 +04:00
|
|
|
$lines[] = $this->_spaces . ' * }';
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = $this->_spaces . ' * )';
|
|
|
|
}
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($associationMapping['orderBy'])) {
|
2011-02-03 01:21:42 +03: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) {
|
2011-05-25 14:35:54 +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);
|
|
|
|
$lines[] = $this->_spaces . ' * })';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$lines[] = $this->_spaces . ' */';
|
|
|
|
|
|
|
|
return implode("\n", $lines);
|
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private function _generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata)
|
2010-03-19 00:38:42 +03:00
|
|
|
{
|
|
|
|
$lines = array();
|
|
|
|
$lines[] = $this->_spaces . '/**';
|
2010-03-20 01:38:45 +03:00
|
|
|
$lines[] = $this->_spaces . ' * @var ' . $fieldMapping['type'] . ' $' . $fieldMapping['fieldName'];
|
2010-03-19 00:38:42 +03:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($this->_generateAnnotations) {
|
|
|
|
$lines[] = $this->_spaces . ' *';
|
|
|
|
|
|
|
|
$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
|
|
|
|
2011-02-03 01:21:42 +03: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']) {
|
2011-02-03 01:21:42 +03:00
|
|
|
$lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Id';
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if ($generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
|
2011-02-03 01:21:42 +03:00
|
|
|
$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'])) {
|
|
|
|
$sequenceGenerator[] = 'allocationSize="' . $metadata->sequenceGeneratorDefinition['allocationSize'] . '"';
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
if (isset($metadata->sequenceGeneratorDefinition['initialValue'])) {
|
|
|
|
$sequenceGenerator[] = 'initialValue="' . $metadata->sequenceGeneratorDefinition['initialValue'] . '"';
|
|
|
|
}
|
2010-03-29 02:56:59 +04:00
|
|
|
|
2011-02-03 01:21:42 +03: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']) {
|
2011-02-03 01:21:42 +03:00
|
|
|
$lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Version';
|
2010-03-19 00:38:42 +03:00
|
|
|
}
|
|
|
|
}
|
2010-03-20 01:38:45 +03:00
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
$lines[] = $this->_spaces . ' */';
|
|
|
|
|
|
|
|
return implode("\n", $lines);
|
|
|
|
}
|
|
|
|
|
2010-03-20 01:38:45 +03:00
|
|
|
private function _prefixCodeWithSpaces($code, $num = 1)
|
|
|
|
{
|
|
|
|
$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) {
|
2010-03-29 02:56:59 +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);
|
|
|
|
}
|
|
|
|
|
2010-03-19 00:38:42 +03:00
|
|
|
private function _getInheritanceTypeString($type)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getChangeTrackingPolicyString($policy)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getIdGeneratorTypeString($type)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|