/** * Generated and write entity class to disk for the given ClassMetadataInfo instance * * @param ClassMetadataInfo $metadata * @param string $outputDirectory * @return void */ public function writeEntityClass(ClassMetadataInfo $metadata, $outputDirectory) { $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension; $dir = dirname($path); if (!is_dir($dir)) { mkdir($dir, 0777, true); } $this->_isNew = !file_exists($path) || file_exists($path) && $this->_regenerateEntityIfExists; if (!$this->_isNew) { $this->_parseTokensInEntityFile(file_get_contents($path)); $this->_staticReflection[$metadata->name]['parent'] = \Includes\Decorator\Utils\Tokenizer::getParentClassName($path); } else { $this->_staticReflection[$metadata->name] = array('properties' => array(), 'methods' => array()); } if ($this->_backupExisting && file_exists($path)) { $backupPath = dirname($path) . DIRECTORY_SEPARATOR . basename($path) . "~"; if (!copy($path, $backupPath)) { throw new \RuntimeException("Attempt to backup overwritten entity file but copy operation failed."); } } // If entity doesn't exist or we're re-generating the entities entirely if ($this->_isNew) { 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)); } } }
/** * Get structures to save when module is disabled * * @param string $author Module author * @param string $name Module name * * @return array */ public static function getModuleProtectedStructures($author, $name) { $tables = array(); $columns = array(); $path = static::getAbsoluteDir($author, $name) . 'Model'; if (\Includes\Utils\FileManager::isExists($path)) { $filter = new \Includes\Utils\FileFilter($path, '/.*\\.php$/Si'); foreach ($filter->getIterator() as $path => $data) { // DO NOT call "getInterfaces()" after the "getFullClassName()" // DO NOT use reflection to get interfaces $interfaces = \Includes\Decorator\Utils\Tokenizer::getInterfaces($path); $class = \Includes\Decorator\Utils\Tokenizer::getFullClassName($path); // Do 'autoload' checking first since the class_exists tries to use autoloader // but fails into "cannot include file" warning when model class is not set to use (LC_Dependencies issue) if (\Includes\Autoloader::checkAutoload($class) && class_exists($class)) { // $reflectionClass = new \ReflectionClass($class); if ($class && is_subclass_of($class, '\\XLite\\Model\\AEntity')) { $class = ltrim($class, '\\'); $len = strlen(\Includes\Utils\Database::getTablesPrefix()); // DO NOT remove leading backslash in interface name if (in_array('\\XLite\\Base\\IDecorator', $interfaces)) { $parent = \Includes\Decorator\Utils\Tokenizer::getParentClassName($path); $parent = ltrim($parent, '\\'); $metadata = \XLite\Core\Database::getEM()->getClassMetadata($parent); $table = substr($metadata->getTableName(), $len); $tool = new \Doctrine\ORM\Tools\SchemaTool(\XLite\Core\Database::getEM()); $schema = $tool->getCreateSchemaSql(array($metadata)); foreach ((array) $metadata->reflFields as $field => $reflection) { $pattern = '/(?:, |\\()(' . $field . ' .+)(?:, [A-Za-z]|\\) ENGINE)/USsi'; if ($reflection->class === $class && !empty($metadata->fieldMappings[$field]) && preg_match($pattern, $schema[0], $matches)) { $columns[$table][$field] = $matches[1]; } } foreach ($metadata->associationMappings as $mapping) { if ($metadata->reflFields[$mapping['fieldName']]->class === $class) { if (isset($mapping['joinTable']) && $mapping['joinTable']) { $tables[] = substr($mapping['joinTable']['name'], $len); } elseif (isset($mapping['joinColumns']) && $mapping['joinColumns']) { foreach ($mapping['joinColumns'] as $col) { $pattern = '/(?:, |\\()(' . $col['name'] . ' .+)(?:, [A-Za-z]|\\) ENGINE)/USsi'; if (preg_match($pattern, $schema[0], $matches)) { $columns[$table][$col['name']] = $matches[1]; } } } } } } elseif (\XLite\Core\Database::getRepo($class)->canDisableTable()) { $tables[] = substr(\XLite\Core\Database::getEM()->getClassMetadata($class)->getTableName(), $len); $metadata = \XLite\Core\Database::getEM()->getClassMetadata($class); foreach ($metadata->associationMappings as $mapping) { if (isset($mapping['joinTable']) && $mapping['joinTable']) { $tables[] = substr($mapping['joinTable']['name'], $len); } } } } } } } return array('tables' => $tables, 'columns' => $columns); }