1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

// namespace and use statements here

class Resource extends HelperAbstract
{

    protected $resourcesMap = array(
        'em' => 'doctrineorm'
    );

    private $resources = array();

    public function direct($name)
    {
        return $this->getResource($name);
    }

    public function getResource($name)
    {
        if(!isset($this->resources[$name])){
            $bootstrap  = $this->getBootstrap();
            $controller = $this->getActionController();

            if(!array_key_exists($name, $this->resourcesMap)){
                throw new \DomainException("Resource $name was not found in the resource map");
            }

            if(!$bootstrap->hasResource($this->resourcesMap[$name])){
                throw new \DomainException("Unable to locate bootstrap resource by name: {$this->resourcesMap[$name]}");
            }

            $this->resources[$name] = $bootstrap->getResource($this->resourcesMap[$name]);
        }

        return $this->resources[$name];
    }

    public function getBootstrap()
    {
        return $this->getFrontController()->getParam('bootstrap');
    }

    public function addResource($name, $resource)
    {
        $this->resources[$name] = $resource;
    }

}