1: <?php
2:
3: /**
4: * TODO: License
5: */
6:
7: namespace Mapbender\CoreBundle;
8:
9: use Mapbender\CoreBundle\Component\Application;
10: use Mapbender\CoreBundle\Component\ApplicationYAMLMapper;
11: use Mapbender\CoreBundle\Entity\Application as Entity;
12: use Symfony\Component\DependencyInjection\ContainerInterface;
13:
14: /**
15: * Mapbender - The central Mapbender3 service. Provides metadata about
16: * available elements, layers and templates.
17: *
18: * @author Christian Wygoda
19: */
20: class Mapbender {
21: private $container;
22: private $elements = array();
23: private $layers = array();
24: private $templates = array();
25: private $repositoryManagers = array();
26:
27: /**
28: * Mapbender constructor.
29: *
30: * Iterate over all bundles and if is an MapbenderBundle, get list
31: * of elements, layers and templates.
32: *
33: * @param ContainerInterface $container
34: */
35: public function __construct(ContainerInterface $container) {
36: $this->container = $container;
37: $bundles = $container->get('kernel')->getBundles();
38: foreach($bundles as $bundle) {
39: if(is_subclass_of($bundle,
40: 'Mapbender\CoreBundle\Component\MapbenderBundle')) {
41:
42: $this->elements = array_merge($this->elements,
43: $bundle->getElements());
44: $this->layer = array_merge($this->layers,
45: $bundle->getLayers());
46: $this->templates = array_merge($this->templates,
47: $bundle->getTemplates());
48: $this->repositoryManagers = array_merge($this->repositoryManagers,
49: $bundle->getRepositoryManagers());
50: }
51: }
52: }
53:
54: /**
55: * Get list of all declared element classes.
56: *
57: * Element classes need to be declared in each bundle's main class getElement
58: * method.
59: *
60: * @return array
61: */
62: public function getElements() {
63: return $this->elements;
64: }
65:
66: /**
67: * Get list of all declared source factories.
68: *
69: * @return array
70: */
71: public function getRepositoryManagers()
72: {
73: return $this->repositoryManagers;
74: }
75:
76: /**
77: * Get list of all declared layer classes.
78: *
79: * Layer classes need to be declared in each bundle's main class getLayers
80: * method.
81: *
82: * @return array
83: */
84: public function getLayers() {
85: return $this->layers;
86: }
87:
88: /**
89: * Get list of all declared template classes.
90: *
91: * Template classes need to be declared in each bundle's main class
92: * getTemplates method.
93: *
94: * @return array
95: */
96: public function getTemplates() {
97: return $this->templates;
98: }
99:
100: /**
101: * Get the application for the given slug.
102: *
103: * Returns either application if it exists, null otherwise. If two
104: * applications with the same slug exist, the database one will
105: * override the YAML one.
106: *
107: * @return Application
108: */
109: public function getApplication($slug, $urls) {
110: $entity = $this->getApplicationEntity($slug);
111: if(!$entity) {
112: return null;
113: }
114:
115: return new Application($this->container, $entity, $urls);
116: }
117:
118: /**
119: * Get application entities
120: *
121: * @return array
122: */
123: public function getApplicationEntities() {
124: $entities = array();
125:
126: $yamlMapper = new ApplicationYAMLMapper($this->container);
127: $yamlEntities = $yamlMapper->getApplications();
128: foreach($yamlEntities as $entity) {
129: $entities[$entity->getSlug()] = $entity;
130: }
131:
132: $dbEntities = $this->container->get('doctrine')
133: ->getRepository('MapbenderCoreBundle:Application')
134: ->findAll();
135: foreach($dbEntities as $entity) {
136: $entity->setSource(Entity::SOURCE_DB);
137: $entities[$entity->getSlug()] = $entity;
138: }
139:
140: return $entities;
141: }
142:
143: /**
144: * Get application entity for given slug
145: *
146: * @return Entity
147: */
148: public function getApplicationEntity($slug) {
149: $entity = $this->container->get('doctrine')
150: ->getRepository('MapbenderCoreBundle:Application')
151: ->findOneBySlug($slug);
152: if($entity) {
153: $entity->setSource(Entity::SOURCE_DB);
154: return $entity;
155: }
156:
157: $yamlMapper = new ApplicationYAMLMapper($this->container);
158: return $yamlMapper->getApplication($slug);
159: }
160:
161: /**
162: * @inheritdoc
163: */
164: public static function getFormTemplate()
165: {
166: return 'MapbenderManagerBundle:Element:map.html.twig';
167: }
168: }
169:
170: