[CLEANUP] MENU Content Object classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / Menu / MenuContentObjectFactory.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject\Menu;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Factory for menu content objects. Allows overriding the default
21 * types like 'GMENU' with an own implementation (only one possible)
22 * and new types can be registered.
23 *
24 * @author Christian Kuhn <lolli@schwarzbu.ch>
25 */
26 class MenuContentObjectFactory implements \TYPO3\CMS\Core\SingletonInterface {
27
28 /**
29 * Register of TypoScript keys to according render class
30 *
31 * @var array
32 */
33 protected $menuTypeToClassMapping = array(
34 'GMENU' => GraphicalMenuContentObject::class,
35 'TMENU' => TextMenuContentObject::class,
36 'IMGMENU' => ImageMenuContentObject::class,
37 'JSMENU' => JavaScriptMenuContentObject::class,
38 );
39
40 /**
41 * Gets a typo script string like 'TMENU' and returns an object of this type
42 *
43 * @param string $type
44 * @return AbstractMenuContentObject Menu object
45 * @throws Exception\NoSuchMenuTypeException
46 */
47 public function getMenuObjectByType($type = '') {
48 $uppercasedClassName = strtoupper($type);
49 if (array_key_exists($uppercasedClassName, $this->menuTypeToClassMapping)) {
50 $object = GeneralUtility::makeInstance($this->menuTypeToClassMapping[$uppercasedClassName]);
51 } else {
52 throw new Exception\NoSuchMenuTypeException(
53 'Menu type ' . (string)$type . ' has no implementing class.',
54 1363278130
55 );
56 }
57 return $object;
58 }
59
60 /**
61 * Register new menu type or override existing type
62 *
63 * @param string $type Menu type to be used in TypoScript
64 * @param string $className Class rendering the menu
65 * @throws \InvalidArgumentException
66 */
67 public function registerMenuType($type, $className) {
68 if (!is_string($type) || !is_string($className)) {
69 throw new \InvalidArgumentException(
70 'type and className must be strings',
71 1363429303
72 );
73 }
74 $this->menuTypeToClassMapping[strtoupper($type)] = $className;
75 }
76
77 }