Skip to content

Commit 50ac81b

Browse files
committed
#12 : Support of PHP 5.3 namespaces
1 parent 17b0810 commit 50ac81b

File tree

4 files changed

+46
-74
lines changed

4 files changed

+46
-74
lines changed

samples/01simple.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
include_once 'Sample_Header.php';
44

5+
use PhpOffice\PhpProject\PHPProject;
6+
57
// Create new PHPProject object
68
echo date('H:i:s') . ' Create new PHPProject object'.EOL;
79
$objPHPProject = new PHPProject();

samples/Sample_Header.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Header file
44
*/
5-
// use PhpOffice\PhpProject\Autoloader;
5+
use PhpOffice\PhpProject\Autoloader;
66
// use PhpOffice\PhpProject\Settings;
77
// use PhpOffice\PhpProject\IOFactory;
88

@@ -13,7 +13,7 @@
1313
define('IS_INDEX', SCRIPT_FILENAME == 'index');
1414

1515
require_once __DIR__ . '/../src/PhpProject/Autoloader.php';
16-
// Autoloader::register();
16+
Autoloader::register();
1717

1818
// Set writers
1919
$writers = array('GanttProject' => 'gan', 'MSProjectExchange' => 'mpx');

src/PhpProject/Autoloader.php

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,55 @@
11
<?php
22
/**
3-
* PHPProject
4-
*
5-
* Copyright (c) 2012 - 2012 PHPProject
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 PHPProject
22-
* @package PHPProject
23-
* @copyright Copyright (c) 2012 - 2012 PHPProject (https://github.com/PHPOffice/PHPProject)
24-
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25-
* @version ##VERSION##, ##DATE##
26-
*/
27-
28-
PHPProject_Autoloader::Register();
29-
// check mbstring.func_overload
30-
if (ini_get('mbstring.func_overload') & 2) {
31-
throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
32-
}
33-
3+
* This file is part of PHPProject - A pure PHP library for reading and writing
4+
* presentations documents.
5+
*
6+
* PHPProject is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPProject
14+
* @copyright 2009-2014 PHPProject contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpProject;
3419

3520
/**
36-
* PHPProject_Autoloader
37-
*
38-
* @category PHPProject
39-
* @package PHPProject
40-
* @copyright Copyright (c) 2006 - 2012 PHPProject (https://github.com/Progi1984/PHPProject)
21+
* Autoloader
4122
*/
42-
class PHPProject_Autoloader
23+
class Autoloader
4324
{
25+
/** @const string */
26+
const NAMESPACE_PREFIX = 'PhpOffice\\PhpProject\\';
27+
4428
/**
45-
* Register the Autoloader with SPL
29+
* Register
4630
*
31+
* @return void
4732
*/
48-
public static function Register() {
49-
if (function_exists('__autoload')) {
50-
// Register any existing autoloader function with SPL, so we don't get any clashes
51-
spl_autoload_register('__autoload');
52-
}
53-
// Register ourselves with SPL
54-
return spl_autoload_register(array('PHPProject_Autoloader', 'Load'));
55-
} // function Register()
56-
33+
public static function register()
34+
{
35+
spl_autoload_register(array(new self, 'autoload'));
36+
}
5737

5838
/**
59-
* Autoload a class identified by name
39+
* Autoload
6040
*
61-
* @param string $pClassName Name of the object to load
41+
* @param string $class
6242
*/
63-
public static function Load($pClassName){
64-
if ((class_exists($pClassName)) || (strpos($pClassName, 'PHPProject') !== 0)) {
65-
// Either already loaded, or not a PHPProject class request
66-
return FALSE;
43+
public static function autoload($class)
44+
{
45+
$prefixLength = strlen(self::NAMESPACE_PREFIX);
46+
if (0 === strncmp(self::NAMESPACE_PREFIX, $class, $prefixLength)) {
47+
$file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
48+
$file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
49+
if (file_exists($file)) {
50+
/** @noinspection PhpIncludeInspection Dynamic includes */
51+
require_once $file;
52+
}
6753
}
68-
69-
$pClassFilePath = PHPPROJECT_ROOT .
70-
str_replace('_',DIRECTORY_SEPARATOR,$pClassName) .
71-
'.php';
72-
73-
if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) {
74-
// Can't load
75-
return FALSE;
76-
}
77-
78-
require($pClassFilePath);
79-
} // function Load()
80-
81-
}
54+
}
55+
}

src/PhpProject/PHPProject.php renamed to src/PhpProject/PhpProject_.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525
* @version ##VERSION##, ##DATE##
2626
*/
2727

28-
/** PHPProject root directory */
29-
if (!defined('PHPPROJECT_ROOT')) {
30-
define('PHPPROJECT_ROOT', dirname(__FILE__) . '/');
31-
require(PHPPROJECT_ROOT . 'PHPProject/Autoloader.php');
32-
}
28+
namespace PhpOffice\PhpProject;
3329

3430
/**
3531
* PHPProject

0 commit comments

Comments
 (0)