Completed
Pull Request — 5.x (#16)
by Lars
05:10
created

Swift::autoload()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.6048
Metric Value
dl 0
loc 22
ccs 7
cts 12
cp 0.5833
rs 8.6738
cc 6
eloc 11
nc 5
nop 1
crap 8.6048
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * General utility class in Swift Mailer, not to be instantiated.
13
 *
14
 *
15
 * @author Chris Corbyn
16
 */
17
abstract class Swift
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
{
19
    const VERSION = '5.x';
20
21
    public static $initialized = false;
22
    public static $inits = array();
23
24
    /**
25
     * Registers an initializer callable that will be called the first time
26
     * a SwiftMailer class is autoloaded.
27
     *
28
     * This enables you to tweak the default configuration in a lazy way.
29
     *
30
     * @param mixed $callable A valid PHP callable that will be called when autoloading the first Swift class
31
     */
32
    public static function init($callable)
33
    {
34
        self::$inits[] = $callable;
35
    }
36
37
    /**
38
     * Internal autoloader for spl_autoload_register().
39
     *
40
     * @param string $class
41
     */
42 62
    public static function autoload($class)
43
    {
44
        // Don't interfere with other autoloaders
45 62
        if (0 !== strpos($class, 'Swift_')) {
46
            return;
47
        }
48
49 62
        $path = __DIR__ . '/' . str_replace('_', '/', $class) . '.php';
50
51 62
        if (!file_exists($path)) {
52
            return;
53
        }
54
55 62
        require $path;
56
57 62
        if (self::$inits && !self::$initialized) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::$inits of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
58
            self::$initialized = true;
59
            foreach (self::$inits as $init) {
60
                call_user_func($init);
61
            }
62
        }
63 62
    }
64
65
    /**
66
     * Configure autoloading using Swift Mailer.
67
     *
68
     * This is designed to play nicely with other autoloaders.
69
     *
70
     * @param mixed $callable A valid PHP callable that will be called when autoloading the first Swift class
71
     */
72
    public static function registerAutoload($callable = null)
73
    {
74
        if (null !== $callable) {
75
            self::$inits[] = $callable;
76
        }
77
        spl_autoload_register(array('Swift', 'autoload'));
78
    }
79
}
80