forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxCodecRegistry.php
More file actions
152 lines (134 loc) · 2.65 KB
/
mxCodecRegistry.php
File metadata and controls
152 lines (134 loc) · 2.65 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Copyright (c) 2006-2013, Gaudenz Alder
*/
class mxCodecRegistry
{
/**
* Class: mxCodecRegistry
*
* A class to register codecs for objects.
*
* Variable: codecs
*
* Maps from constructor names to codecs.
*/
public static $codecs = array();
/**
* Variable: aliases
*
* Maps from classnames to codecnames.
*/
public static $aliases = array();
/**
* Function: register
*
* Registers a new codec and associates the name of the template constructor
* in the codec with the codec object. Automatically creates an alias if the
* codename and the classname are not equal.
*
* Parameters:
*
* codec - <mxObjectCodec> to be registered.
*/
static function register($codec)
{
if (isset($codec))
{
$name = $codec->getName();
mxCodecRegistry::$codecs[$name] = $codec;
$classname = mxCodecRegistry::getName($codec->template);
if ($classname != $name)
{
mxCodecRegistry::addAlias($classname, $name);
}
}
return $codec;
}
/**
* Function: addAlias
*
* Adds an alias for mapping a classname to a codecname.
*/
static function addAlias($classname, $codecname)
{
mxCodecRegistry::$aliases[$classname] = $codecname;
}
/**
* Function: getCodec
*
* Returns a codec that handles objects that are constructed
* using the given ctor.
*
* Parameters:
*
* ctor - JavaScript constructor function.
*/
static function getCodec($name)
{
$codec = null;
if (isset($name))
{
if (isset(mxCodecRegistry::$aliases[$name]))
{
$tmp = mxCodecRegistry::$aliases[$name];
if (strlen($tmp) > 0)
{
$name = $tmp;
}
}
$codec = (isset(mxCodecRegistry::$codecs[$name])) ?
mxCodecRegistry::$codecs[$name] : null;
// Registers a new default codec for the given constructor
// if no codec has been previously defined.
if (!isset($codec))
{
try
{
$obj = mxCodecRegistry::getInstanceForName($name);
if (isset($obj))
{
$codec = new mxObjectCodec($obj);
mxCodecRegistry::register($codec);
}
}
catch (Exception $e)
{
// ignore
}
}
}
return $codec;
}
/**
* Function: getInstanceForName
*
* Creates and returns a new instance for the given class name.
*/
static function getInstanceForName($name)
{
if (class_exists($name))
{
return new $name();
}
return null;
}
/**
* Function: getName
*
* Returns the codec name for the given object instance.
*
* Parameters:
*
* obj - PHP object to return the codec name for.
*/
static function getName($obj)
{
if (is_array($obj))
{
return "Array";
}
return get_class($obj);
}
}
?>