Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 108 lines (82 sloc) 2.391 kb
fc5c541b »
2011-04-22 [Form] Reorganized code into "form extensions"
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form;
13
14 use Symfony\Component\Form\Exception\FormException;
15 use Symfony\Component\Form\Exception\UnexpectedTypeException;
16
17 /**
18 * @author Bernhard Schussek <bernhard.schussek@symfony.com>
19 */
20 abstract class AbstractExtension implements FormExtensionInterface
21 {
22 /**
23 * @var array
24 */
25 private $types;
26
27 /**
28 * @var Boolean
29 */
30 private $typesLoaded = false;
31
32 /**
33 * @var FormTypeGuesserInterface
34 */
35 private $typeGuesser;
36
37 /**
38 * @var Boolean
39 */
40 private $typeGuesserLoaded = false;
41
42 abstract protected function loadTypes();
43
44 abstract protected function loadTypeGuesser();
45
46 private function initTypes()
47 {
48 $this->typesLoaded = true;
49
50 $types = $this->loadTypes();
51 $typesByName = array();
52
53 foreach ($types as $type) {
54 if (!$type instanceof FormTypeInterface) {
55 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
56 }
57
58 $typesByName[$type->getName()] = $type;
59 }
60
61 $this->types = $typesByName;
62 }
63
64 private function initTypeGuesser()
65 {
66 $this->typeGuesserLoaded = true;
67
68 $guesser = $this->loadTypeGuesser();
69
70 if (!$guesser instanceof FormTypeGuesserInterface) {
71 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeGuesserInterface');
72 }
73
74 $this->guesser = $guesser;
75 }
76
77 public function getType($name)
78 {
79 if (!$this->typesLoaded) {
80 $this->initTypes();
81 }
82
83 if (!isset($this->types[$name])) {
84 throw new FormException(sprintf('The type "%s" can not be typesLoaded by this extension', $name));
85 }
86
87 return $this->types[$name];
88 }
89
90 public function hasType($name)
91 {
92 if (!$this->typesLoaded) {
93 $this->initTypes();
94 }
95
96 return isset($this->types[$name]);
97 }
98
99 public function getTypeGuesser()
100 {
101 if (!$this->typeGuesserLoaded) {
102 $this->initTypeGuesser();
103 }
104
105 return $this->typeGuesser;
106 }
107 }
Something went wrong with that request. Please try again.