~rmwenyekheri/sarisiae/trunk

« back to all changes in this revision

Viewing changes to Classes/PHPExcel/IOFactory.php

  • Committer: NDIMBOJr
  • Date: 2015-08-13 10:09:13 UTC
  • Revision ID: rmwenyekheri@gmail.com-20150813100913-wa5o4k7vvarmzl4n
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * PHPExcel
 
4
 *
 
5
 * Copyright (c) 2006 - 2011 PHPExcel
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 *
 
21
 * @category   PHPExcel
 
22
 * @package    PHPExcel
 
23
 * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
 
24
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 
25
 * @version    1.7.6, 2011-02-27
 
26
 */
 
27
 
 
28
 
 
29
/**     PHPExcel root directory */
 
30
if (!defined('PHPEXCEL_ROOT')) {
 
31
        /**
 
32
         *      @ignore
 
33
         */
 
34
        define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
 
35
        require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
 
36
}
 
37
 
 
38
/**
 
39
 * PHPExcel_IOFactory
 
40
 *
 
41
 * @category   PHPExcel
 
42
 * @package    PHPExcel
 
43
 * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
 
44
 */
 
45
class PHPExcel_IOFactory
 
46
{
 
47
        /**
 
48
         *      Search locations
 
49
         *
 
50
         *      @var    array
 
51
         *      @access private
 
52
         *      @static
 
53
         */
 
54
        private static $_searchLocations = array(
 
55
                array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
 
56
                array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
 
57
        );
 
58
 
 
59
        /**
 
60
         *      Autoresolve classes
 
61
         *
 
62
         *      @var    array
 
63
         *      @access private
 
64
         *      @static
 
65
         */
 
66
        private static $_autoResolveClasses = array(
 
67
                'Excel2007',
 
68
                'Excel5',
 
69
                'Excel2003XML',
 
70
                'OOCalc',
 
71
                'SYLK',
 
72
                'Gnumeric',
 
73
                'CSV',
 
74
        );
 
75
 
 
76
    /**
 
77
     *  Private constructor for PHPExcel_IOFactory
 
78
     */
 
79
    private function __construct() { }
 
80
 
 
81
    /**
 
82
     *  Get search locations
 
83
     *
 
84
         *      @static
 
85
         *      @access public
 
86
     *  @return array
 
87
     */
 
88
        public static function getSearchLocations() {
 
89
                return self::$_searchLocations;
 
90
        }       //      function getSearchLocations()
 
91
 
 
92
        /**
 
93
         *      Set search locations
 
94
         *
 
95
         *      @static
 
96
         *      @access public
 
97
         *      @param  array $value
 
98
         *      @throws Exception
 
99
         */
 
100
        public static function setSearchLocations($value) {
 
101
                if (is_array($value)) {
 
102
                        self::$_searchLocations = $value;
 
103
                } else {
 
104
                        throw new Exception('Invalid parameter passed.');
 
105
                }
 
106
        }       //      function setSearchLocations()
 
107
 
 
108
        /**
 
109
         *      Add search location
 
110
         *
 
111
         *      @static
 
112
         *      @access public
 
113
         *      @param  string $type            Example: IWriter
 
114
         *      @param  string $location        Example: PHPExcel/Writer/{0}.php
 
115
         *      @param  string $classname       Example: PHPExcel_Writer_{0}
 
116
         */
 
117
        public static function addSearchLocation($type = '', $location = '', $classname = '') {
 
118
                self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
 
119
        }       //      function addSearchLocation()
 
120
 
 
121
        /**
 
122
         *      Create PHPExcel_Writer_IWriter
 
123
         *
 
124
         *      @static
 
125
         *      @access public
 
126
         *      @param  PHPExcel $phpExcel
 
127
         *      @param  string  $writerType     Example: Excel2007
 
128
         *      @return PHPExcel_Writer_IWriter
 
129
         *      @throws Exception
 
130
         */
 
131
        public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
 
132
                // Search type
 
133
                $searchType = 'IWriter';
 
134
 
 
135
                // Include class
 
136
                foreach (self::$_searchLocations as $searchLocation) {
 
137
                        if ($searchLocation['type'] == $searchType) {
 
138
                                $className = str_replace('{0}', $writerType, $searchLocation['class']);
 
139
                                $classFile = str_replace('{0}', $writerType, $searchLocation['path']);
 
140
 
 
141
                                $instance = new $className($phpExcel);
 
142
                                if (!is_null($instance)) {
 
143
                                        return $instance;
 
144
                                }
 
145
                        }
 
146
                }
 
147
 
 
148
                // Nothing found...
 
149
                throw new Exception("No $searchType found for type $writerType");
 
150
        }       //      function createWriter()
 
151
 
 
152
        /**
 
153
         *      Create PHPExcel_Reader_IReader
 
154
         *
 
155
         *      @static
 
156
         *      @access public
 
157
         *      @param  string $readerType      Example: Excel2007
 
158
         *      @return PHPExcel_Reader_IReader
 
159
         *      @throws Exception
 
160
         */
 
161
        public static function createReader($readerType = '') {
 
162
                // Search type
 
163
                $searchType = 'IReader';
 
164
 
 
165
                // Include class
 
166
                foreach (self::$_searchLocations as $searchLocation) {
 
167
                        if ($searchLocation['type'] == $searchType) {
 
168
                                $className = str_replace('{0}', $readerType, $searchLocation['class']);
 
169
                                $classFile = str_replace('{0}', $readerType, $searchLocation['path']);
 
170
 
 
171
                                $instance = new $className();
 
172
                                if (!is_null($instance)) {
 
173
                                        return $instance;
 
174
                                }
 
175
                        }
 
176
                }
 
177
 
 
178
                // Nothing found...
 
179
                throw new Exception("No $searchType found for type $readerType");
 
180
        }       //      function createReader()
 
181
 
 
182
        /**
 
183
         *      Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
 
184
         *
 
185
         *      @static
 
186
         *      @access public
 
187
         *      @param  string          $pFileName
 
188
         *      @return PHPExcel
 
189
         *      @throws Exception
 
190
         */
 
191
        public static function load($pFilename) {
 
192
                $reader = self::createReaderForFile($pFilename);
 
193
                return $reader->load($pFilename);
 
194
        }       //      function load()
 
195
 
 
196
        /**
 
197
         *      Identify file type using automatic PHPExcel_Reader_IReader resolution
 
198
         *
 
199
         *      @static
 
200
         *      @access public
 
201
         *      @param  string          $pFileName
 
202
         *      @return string
 
203
         *      @throws Exception
 
204
         */
 
205
        public static function identify($pFilename) {
 
206
                $reader = self::createReaderForFile($pFilename);
 
207
                $className = get_class($reader);
 
208
                $classType = explode('_',$className);
 
209
                unset($reader);
 
210
                return array_pop($classType);
 
211
        }       //      function identify()
 
212
 
 
213
        /**
 
214
         *      Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
 
215
         *
 
216
         *      @static
 
217
         *      @access public
 
218
         *      @param  string          $pFileName
 
219
         *      @return PHPExcel_Reader_IReader
 
220
         *      @throws Exception
 
221
         */
 
222
        public static function createReaderForFile($pFilename) {
 
223
 
 
224
                // First, lucky guess by inspecting file extension
 
225
                $pathinfo = pathinfo($pFilename);
 
226
 
 
227
                if (isset($pathinfo['extension'])) {
 
228
                        switch (strtolower($pathinfo['extension'])) {
 
229
                                case 'xlsx':
 
230
                                        $reader = self::createReader('Excel2007');
 
231
                                        break;
 
232
                                case 'xls':
 
233
                                        $reader = self::createReader('Excel5');
 
234
                                        break;
 
235
                                case 'ods':
 
236
                                        $reader = self::createReader('OOCalc');
 
237
                                        break;
 
238
                                case 'slk':
 
239
                                        $reader = self::createReader('SYLK');
 
240
                                        break;
 
241
                                case 'xml':
 
242
                                        $reader = self::createReader('Excel2003XML');
 
243
                                        break;
 
244
                                case 'gnumeric':
 
245
                                        $reader = self::createReader('Gnumeric');
 
246
                                        break;
 
247
                                case 'csv':
 
248
                                        // Do nothing
 
249
                                        // We must not try to use CSV reader since it loads
 
250
                                        // all files including Excel files etc.
 
251
                                        break;
 
252
                                default:
 
253
                                        break;
 
254
                        }
 
255
 
 
256
                        // Let's see if we are lucky
 
257
                        if (isset($reader) && $reader->canRead($pFilename)) {
 
258
                                return $reader;
 
259
                        }
 
260
 
 
261
                }
 
262
 
 
263
                // If we reach here then "lucky guess" didn't give any result
 
264
 
 
265
                // Try loading using self::$_autoResolveClasses
 
266
                foreach (self::$_autoResolveClasses as $autoResolveClass) {
 
267
                        $reader = self::createReader($autoResolveClass);
 
268
                        if ($reader->canRead($pFilename)) {
 
269
                                return $reader;
 
270
                        }
 
271
                }
 
272
 
 
273
        }       //      function createReaderForFile()
 
274
}