[TASK] Separate ext:statictemplates code from core
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / Menu / MenuContentObjectFactory.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject\Menu;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Factory for menu content objects. Allows overriding the default
32 * types like 'GMENU' with an own implementation (only one possible)
33 * and new types can be registered.
34 *
35 * @author Christian Kuhn <lolli@schwarzbu.ch>
36 */
37 class MenuContentObjectFactory implements \TYPO3\CMS\Core\SingletonInterface {
38
39 /**
40 * Register of TypoScript keys to according render class
41 *
42 * @var array
43 */
44 protected $menuTypeToClassMapping = array(
45 'GMENU' => 'TYPO3\\CMS\\Frontend\\ContentObject\\Menu\\GraphicalMenuContentObject',
46 'TMENU' => 'TYPO3\\CMS\\Frontend\\ContentObject\\Menu\\TextMenuContentObject',
47 'IMGMENU' => 'TYPO3\\CMS\\Frontend\\ContentObject\\Menu\\ImageMenuContentObject',
48 'JSMENU' => 'TYPO3\\CMS\\Frontend\\ContentObject\\Menu\\JavaScriptMenuContentObject',
49 );
50
51 /**
52 * Gets a typo script string like 'TMENU' and returns an object of this type
53 *
54 * @param string $type
55 * @return object Menu object
56 * @throws Exception\NoSuchMenuTypeException
57 */
58 public function getMenuObjectByType($type = '') {
59 $uppercasedClassname = strtoupper($type);
60 if (array_key_exists($uppercasedClassname, $this->menuTypeToClassMapping)) {
61 $object = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($this->menuTypeToClassMapping[$uppercasedClassname]);
62 } else {
63 throw new Exception\NoSuchMenuTypeException(
64 'Menu type ' . (string)$type . ' has no implementing class.',
65 1363278130
66 );
67 }
68 return $object;
69 }
70
71 /**
72 * Register new menu type or override existing type
73 *
74 * @param string $type Menu type to be used in TypoScript
75 * @param string $className Class rendering the menu
76 * @throws \InvalidArgumentException
77 */
78 public function registerMenuType($type, $className) {
79 if (!is_string($type) || !is_string($className)) {
80 throw new \InvalidArgumentException(
81 'type and className must be strings',
82 1363429303
83 );
84 }
85 $this->menuTypeToClassMapping[strtoupper($type)] = $className;
86 }
87 }
88
89 ?>