1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\ORM\Tools\Export\Driver;
21
22use Doctrine\ORM\Mapping\ClassMetadataInfo;
23use Doctrine\ORM\Tools\Export\ExportException;
24
25/**
26 * Abstract base class which is to be used for the Exporter drivers
27 * which can be found in \Doctrine\ORM\Tools\Export\Driver.
28 *
29 * @link    www.doctrine-project.org
30 * @since   2.0
31 * @author  Jonathan Wage <jonwage@gmail.com>
32 */
33abstract class AbstractExporter
34{
35    /**
36     * @var array
37     */
38    protected $_metadata = array();
39
40    /**
41     * @var string|null
42     */
43    protected $_outputDir;
44
45    /**
46     * @var string|null
47     */
48    protected $_extension;
49
50    /**
51     * @var bool
52     */
53    protected $_overwriteExistingFiles = false;
54
55    /**
56     * @param string|null $dir
57     */
58    public function __construct($dir = null)
59    {
60        $this->_outputDir = $dir;
61    }
62
63    /**
64     * @param bool $overwrite
65     *
66     * @return void
67     */
68    public function setOverwriteExistingFiles($overwrite)
69    {
70        $this->_overwriteExistingFiles = $overwrite;
71    }
72
73    /**
74     * Converts a single ClassMetadata instance to the exported format
75     * and returns it.
76     *
77     * @param ClassMetadataInfo $metadata
78     *
79     * @return string
80     */
81    abstract public function exportClassMetadata(ClassMetadataInfo $metadata);
82
83    /**
84     * Sets the array of ClassMetadataInfo instances to export.
85     *
86     * @param array $metadata
87     *
88     * @return void
89     */
90    public function setMetadata(array $metadata)
91    {
92        $this->_metadata = $metadata;
93    }
94
95    /**
96     * Gets the extension used to generated the path to a class.
97     *
98     * @return string|null
99     */
100    public function getExtension()
101    {
102        return $this->_extension;
103    }
104
105    /**
106     * Sets the directory to output the mapping files to.
107     *
108     *     [php]
109     *     $exporter = new YamlExporter($metadata);
110     *     $exporter->setOutputDir(__DIR__ . '/yaml');
111     *     $exporter->export();
112     *
113     * @param string $dir
114     *
115     * @return void
116     */
117    public function setOutputDir($dir)
118    {
119        $this->_outputDir = $dir;
120    }
121
122    /**
123     * Exports each ClassMetadata instance to a single Doctrine Mapping file
124     * named after the entity.
125     *
126     * @return void
127     *
128     * @throws \Doctrine\ORM\Tools\Export\ExportException
129     */
130    public function export()
131    {
132        if ( ! is_dir($this->_outputDir)) {
133            mkdir($this->_outputDir, 0775, true);
134        }
135
136        foreach ($this->_metadata as $metadata) {
137            // In case output is returned, write it to a file, skip otherwise
138            if($output = $this->exportClassMetadata($metadata)){
139                $path = $this->_generateOutputPath($metadata);
140                $dir = dirname($path);
141                if ( ! is_dir($dir)) {
142                    mkdir($dir, 0775, true);
143                }
144                if (file_exists($path) && !$this->_overwriteExistingFiles) {
145                    throw ExportException::attemptOverwriteExistingFile($path);
146                }
147                file_put_contents($path, $output);
148                chmod($path, 0664);
149            }
150        }
151    }
152
153    /**
154     * Generates the path to write the class for the given ClassMetadataInfo instance.
155     *
156     * @param ClassMetadataInfo $metadata
157     *
158     * @return string
159     */
160    protected function _generateOutputPath(ClassMetadataInfo $metadata)
161    {
162        return $this->_outputDir . '/' . str_replace('\\', '.', $metadata->name) . $this->_extension;
163    }
164
165    /**
166     * Sets the directory to output the mapping files to.
167     *
168     *     [php]
169     *     $exporter = new YamlExporter($metadata, __DIR__ . '/yaml');
170     *     $exporter->setExtension('.yml');
171     *     $exporter->export();
172     *
173     * @param string $extension
174     *
175     * @return void
176     */
177    public function setExtension($extension)
178    {
179        $this->_extension = $extension;
180    }
181
182    /**
183     * @param int $type
184     *
185     * @return string
186     */
187    protected function _getInheritanceTypeString($type)
188    {
189        switch ($type) {
190            case ClassMetadataInfo::INHERITANCE_TYPE_NONE:
191                return 'NONE';
192
193            case ClassMetadataInfo::INHERITANCE_TYPE_JOINED:
194                return 'JOINED';
195
196            case ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE:
197                return 'SINGLE_TABLE';
198
199            case ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS:
200                return 'PER_CLASS';
201        }
202    }
203
204    /**
205     * @param int $mode
206     *
207     * @return string
208     */
209    protected function _getFetchModeString($mode)
210    {
211        switch ($mode) {
212            case ClassMetadataInfo::FETCH_EAGER:
213                return 'EAGER';
214
215            case ClassMetadataInfo::FETCH_EXTRA_LAZY:
216                return 'EXTRA_LAZY';
217
218            case ClassMetadataInfo::FETCH_LAZY:
219                return 'LAZY';
220        }
221    }
222
223    /**
224     * @param int $policy
225     *
226     * @return string
227     */
228    protected function _getChangeTrackingPolicyString($policy)
229    {
230        switch ($policy) {
231            case ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT:
232                return 'DEFERRED_IMPLICIT';
233
234            case ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT:
235                return 'DEFERRED_EXPLICIT';
236
237            case ClassMetadataInfo::CHANGETRACKING_NOTIFY:
238                return 'NOTIFY';
239        }
240    }
241
242    /**
243     * @param int $type
244     *
245     * @return string
246     */
247    protected function _getIdGeneratorTypeString($type)
248    {
249        switch ($type) {
250            case ClassMetadataInfo::GENERATOR_TYPE_AUTO:
251                return 'AUTO';
252
253            case ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE:
254                return 'SEQUENCE';
255
256            case ClassMetadataInfo::GENERATOR_TYPE_TABLE:
257                return 'TABLE';
258
259            case ClassMetadataInfo::GENERATOR_TYPE_IDENTITY:
260                return 'IDENTITY';
261
262            case ClassMetadataInfo::GENERATOR_TYPE_UUID:
263                return 'UUID';
264
265            case ClassMetadataInfo::GENERATOR_TYPE_CUSTOM:
266                return 'CUSTOM';
267        }
268    }
269}
270