/
TypeList.php
132 lines (119 loc) · 3.94 KB
/
TypeList.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Concrete\Core\File\Type;
use Concrete\Core\File\Type\Type as FileType;
use Concrete\Core\Support\Facade\Application;
use stdClass;
/**
* @author Andrew Embler <andrew@concrete5.org>
* @copyright Copyright (c) 2003-2009 Concrete5. (http://www.concrete5.org)
* @license http://www.concrete5.org/license/ MIT License
*/
class TypeList
{
/**
* @return TypeList
*/
public static function getInstance()
{
static $instance;
if (!isset($instance)) {
$v = __CLASS__;
$instance = new $v();
}
return $instance;
}
/**
* @var FileType[]
*/
protected $types = [];
/**
* @var stdClass[]
*/
protected $importerAttributes = [];
/**
* Register a file type.
*
* @param string $extension Comma-separated list of file extensions (lower case and without leading dots)
* @param string $name File type name
* @param int $type Generic file type (one of the \Concrete\Core\File\Type\Type\Type::T_... constants)
* @param string $customImporter The handle of the custom importer
* @param string $inlineFileViewer The handle of the inline viewer
* @param string $editor The hHandle of the editor
* @param string $pkgHandle The handle of the owner package
*/
public function define($extension, $name, $type, $customImporter = '', $inlineFileViewer = '', $editor = '', $pkgHandle = '')
{
$ext = explode(',', $extension);
foreach ($ext as $e) {
$this->types[strtolower($e)] = (new FileType())
->setName($name)
->setExtension($e)
->setCustomImporter($customImporter)
->setEditor($editor)
->setGenericType($type)
->setView($inlineFileViewer)
->setPackageHandle($pkgHandle);
}
}
/**
* Register multiple file types.
*
* @param array $types Keys are the type names, values are the other parameters accepted by the define() method
*/
public function defineMultiple(array $types)
{
foreach ($types as $type_name => $type_settings) {
array_splice($type_settings, 1, 0, $type_name);
call_user_func_array([$this, 'define'], $type_settings);
}
}
public function defineImporterAttribute($akHandle, $akName, $akType, $akIsEditable)
{
$obj = new stdClass();
$obj->akHandle = $akHandle;
$obj->akName = $akName;
$obj->akType = $akType;
$obj->akIsEditable = $akIsEditable;
$this->importerAttributes[$akHandle] = $obj;
}
public function defineImporterAttributeMultiple(array $importer_attributes)
{
foreach ($importer_attributes as $attribute_name => $attribute_settings) {
array_unshift($attribute_settings, $attribute_name);
call_user_func_array([$this, 'defineImporterAttribute'], $attribute_settings);
}
}
/**
* @param string $akHandle
*
* @return stdClass|null
*/
public static function getImporterAttribute($akHandle)
{
$ftl = static::getInstance();
return isset($ftl->importerAttributes[$akHandle]) ? $ftl->importerAttributes[$akHandle] : null;
}
/**
* Can take an extension or a filename
* Returns any registered information we have for the particular file type, based on its registration.
*
* @return FileType
*/
public static function getType($ext)
{
$ftl = static::getInstance();
if (strpos($ext, '.') !== false) {
// filename
$app = Application::getFacadeApplication();
$h = $app->make('helper/file');
$ext = $h->getExtension($ext);
}
$ext = strtolower($ext);
if (isset($ftl->types[$ext])) {
return $ftl->types[$ext];
} else {
$ft = new FileType(); // generic
return $ft;
}
}
}