1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Database\Schema;
13:
14: use Doctrine\DBAL\Schema\Table;
15: use Doctrine\DBAL\Schema\Schema;
16: use Doctrine\DBAL\Schema\Sequence;
17:
18: /**
19: * PrefixStripper extends Schema so we can easily add tables and
20: * sequences selectively while visiting another schema.
21: *
22: * New schema will be stripped of database and prefix and optionally
23: * filered by a table list
24: *
25: * @category Xoops\Core\Database\Schema\PrefixStripper
26: * @package Xoops\Core
27: * @author Richard Griffith <richard@geekwright.com>
28: * @copyright 2013 XOOPS Project (http://xoops.org)
29: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
30: * @version Release: 2.6
31: * @link http://xoops.org
32: * @since 2.6.0
33: */
34: class PrefixStripper extends Schema
35: {
36:
37: private $xPrefix = '';
38: private $xDbName = '';
39: private $tableList = array();
40:
41: /**
42: * constructor
43: */
44: public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
45: {
46: $this->xPrefix = strtolower(\XoopsBaseConfig::get('db-prefix') . '_');
47: $this->xDbName = strtolower(\XoopsBaseConfig::get('db-name'));
48: parent::__construct($tables, $sequences, $schemaConfig);
49: }
50:
51: /**
52: * set list of tables to limit schema
53: *
54: * If no list is specified, all tables will be included
55: *
56: * @param array $tableList list of tables to include
57: *
58: * @return void
59: */
60: public function setTableFilter(array $tableList)
61: {
62: $this->tableList = $tableList;
63: }
64:
65: /**
66: * Add a table object to the schema
67: *
68: * @param Table $table table object to add
69: *
70: * @return void
71: */
72: public function addTable(Table $table)
73: {
74: //echo '<h2>addTable()</h2>';
75: try {
76: $name = $table->getName();
77: $len = strlen($this->xPrefix);
78: if (substr_compare($name, $this->xPrefix, 0, $len)===0) {
79: $name = substr($name, $len);
80: if (empty($this->tableList) || in_array($name, $this->tableList)) {
81: $idGeneratorType = 0; // how should we handle this?
82: $newtable = new Table(
83: $name,
84: $table->getColumns(),
85: $table->getIndexes(),
86: $table->getForeignKeys(),
87: $idGeneratorType,
88: $table->getOptions()
89: );
90: $this->_addTable($newtable);
91: }
92: }
93: //Debug::dump($table);
94: } catch (\Exception $e) {
95: \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
96: throw $e;
97: }
98: }
99:
100: /**
101: * Add a sequence to the schema
102: *
103: * @param Sequence $sequence a sequence
104: *
105: * @return void
106: */
107: public function addSequence(Sequence $sequence)
108: {
109: //echo '<h2>addSequence()</h2>';
110: try {
111: $this->_addSequence($sequence);
112: //Debug::dump($sequence);
113: } catch (\Exception $e) {
114: \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
115: throw $e;
116: }
117: }
118: }
119: