11762 use doctrine for schema creation and update
[tine20] / tine20 / Setup / SchemaTool.php
1 <?php
2 /**
3  * Tine 2.0
4  *
5  * @package     Setup
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
7  * @copyright   Copyright (c) 2016 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Cornelius Weiss <c.weiss@metaways.de>
9  */
10
11 use Doctrine\ORM\Tools\Setup;
12 use Doctrine\ORM\EntityManager;
13 use \Doctrine\ORM\Tools\SchemaTool;
14 use Doctrine\Common\Persistence\Mapping\StaticReflectionService;
15 use \Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver;
16
17 /**
18  * helper around docrine2/dbal schema tools
19  */
20 class Setup_SchemaTool
21 {
22     /**
23      * convert tine20config to dbal config
24      *
25      * @return array
26      */
27     public static function getDBParams()
28     {
29         $dbParams = Tinebase_Config::getInstance()->get('database')->toArray();
30         $dbParams['driver'] = $dbParams['adapter'];
31         $dbParams['user'] = $dbParams['username'];
32
33         return $dbParams;
34     }
35
36     public static function getConfig($appName, $modelNames=null)
37     {
38         $mappingDriver = new Tinebase_Record_DoctrineMappingDriver();
39
40         if (! $modelNames) {
41             $modelNames = array();
42
43             foreach($mappingDriver->getAllClassNames() as $modelName) {
44                 $modelConfig = $modelName::getConfiguration();
45
46                 if ($modelConfig->getApplName() == $appName) {
47                     $modelNames[] = $modelName;
48                 }
49             }
50         }
51
52         $tableNames = array();
53         foreach($modelNames as $modelName) {
54             $modelConfig = $modelName::getConfiguration();
55             if (! $mappingDriver->isTransient($modelName)) {
56                 throw new Setup_Exception('Model not yet doctrine2 ready');
57             }
58             $tableNames[] = SQL_TABLE_PREFIX . Tinebase_Helper::array_value('name', $modelConfig->getTable());
59         }
60
61         $config = Setup::createConfiguration();
62         $config->setMetadataDriverImpl($mappingDriver);
63
64         $config->setFilterSchemaAssetsExpression('/'. implode('|',$tableNames) . '/');
65
66         return $config;
67
68     }
69
70     public static function getEntityManager($appName, $modelNames=null)
71     {
72         $em = EntityManager::create(self::getDBParams(), self::getConfig($appName, $modelNames));
73
74         // needed to prevent runtime reflection that needs private properties ...
75         $em->getMetadataFactory()->setReflectionService(new StaticReflectionService());
76
77         return $em;
78     }
79
80     public static function getMetadata($appName, $modelNames=null)
81     {
82         $em = self::getEntityManager($appName, $modelNames);
83
84         $classes = array();
85         foreach($modelNames as $modelName) {
86             $classes[] = $em->getClassMetadata($modelName);
87         }
88
89         return $classes;
90     }
91
92     public static function createSchema($appName, $modelNames=null)
93     {
94         $em = self::getEntityManager($appName, $modelNames);
95         $tool = new SchemaTool($em);
96         $classes = self::getMetadata($appName, $modelNames);
97
98         $tool->createSchema($classes);
99     }
100
101     public static function updateSchema($appName, $modelNames=null)
102     {
103         $em = self::getEntityManager($appName, $modelNames);
104         $tool = new SchemaTool($em);
105         $classes = self::getMetadata($appName, $modelNames);
106
107         $tool->updateSchema($classes, true);
108     }
109 }