Entrypoint::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
/**
3
 * The base Class and super public Interface of this Library
4
 *
5
 * LICENSE: Free for all
6
 *
7
 * @category   Dependency Injection
8
 * @package    Puice
9
 * @copyright  Copyright (c) 2014 Alwin Mark
10
 * @license    Free
11
 * @link       https://github.com/CansaSCityShuffle/Puice
12
 */
13
14
15
namespace Puice;
16
17
use Puice;
18
use Puice\Factory;
19
use Puice\Container;
20
21
/**
22
 * Little Helper Class, to easyfy the useage of Puice.
23
 * Every EntryPoint Class (Controller, Services, ...) can implement this
24
 * and therefore provide a create method which will automatically search
25
 * all dependencies, using the Puice Factory
26
 *
27
 * @category   Dependency Injection
28
 * @package    Puice
29
 * @copyright  Copyright (c) 2014 Alwin Mark
30
 */
31
class Entrypoint
32
{
33
    protected $_puice = null;
34
35
    /**
36
     * will internally call the constructor of the implementing Class with
37
     * predefined Dependencies
38
     *
39
     * @return an instance
40
     */
41
    public static function create()
42
    {
43
        $clazz = get_called_class();
44
        $appConfig = $clazz::getConfigInstance();
45
        $myConfig = $clazz::getConfigInstance();
46
        $puice = new Puice($appConfig, $myConfig);
47
48
        self::loadConfig($clazz::getAppConf(), $appConfig, $puice);
49
        self::loadConfig($clazz::getMyConf(), $myConfig, $puice);
50
51
        $object = $puice->create($clazz);
52
        $object->setPuice($puice);
53
54
        return $object;
55
    }
56
57
    /**
58
     * loadsConfigfile
59
     * If the configPath == null nothing will be loaded
60
     *
61
     * @param $configPath           path to the required Configfile
62
     * @param Puice\Config &$config reference to the Configuration Object
63
     *                              that should be filled
64
     * @param Puice $puice          Puice object, so its possible to create
65
     *                              "singleton" Objects in the Config
66
     */
67
    private static function loadConfig(
68
        $configPath, Config &$config, Puice $puice
69
    ) {
70
        if ($configPath != null) {
71
            include $configPath;
72
        }
73
    }
74
75
    /**
76
     * Hookfunction to enable the usageo of Costum Implementations of
77
     * Puice\Config
78
     *
79
     * @return Puice\Config
80
     */
81
    protected static function getConfigInstance()
82
    {
83
        return new Puice\Config\DefaultConfig();
84
    }
85
86
    /**
87
     * Overwriteable Function to custom define the Root Application
88
     * Config file for Puice
89
     *
90
     * @see features/puice.feature
91
     *
92
     * @return string Path of the root Application Puice Configfile
93
     */
94
    protected static function getAppConf()
95
    {
96
        return $_SERVER['APP_CONFIG'];
97
    }
98
99
    /**
100
     * Overwriteable Function to custom define the EntryPoint
101
     * Config file for Puice
102
     *
103
     * @see features/puice.feature
104
     *
105
     * @return string Path of the Entrypoint Puice Configfile
106
     */
107
    protected static function getMyConf()
108
    {
109
        $className = str_replace('\\', '_', get_called_class());
110
        $key = "{$className}_CONFIG";
111
        if (array_key_exists($key, $_SERVER)) {
112
            return $_SERVER[$key];
113
        }
114
115
        return null;
116
    }
117
118
    /**
119
     * setter for Puice
120
     *
121
     * @param $factory
122
     */
123
    public function setPuice(Puice $puice)
124
    {
125
        $this->_puice = $puice;
126
    }
127
}
128