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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | <?php namespace DrSlump; use DrSlump\Protobuf; class Protobuf { const VERSION = '@package_version@'; const RULE_OPTIONAL = 1; const RULE_REQUIRED = 2; const RULE_REPEATED = 3; const RULE_UNKNOWN = -1; const TYPE_DOUBLE = 1; const TYPE_FLOAT = 2; const TYPE_INT64 = 3; const TYPE_UINT64 = 4; const TYPE_INT32 = 5; const TYPE_FIXED64 = 6; const TYPE_FIXED32 = 7; const TYPE_BOOL = 8; const TYPE_STRING = 9; const TYPE_GROUP = 10; const TYPE_MESSAGE = 11; const TYPE_BYTES = 12; const TYPE_UINT32 = 13; const TYPE_ENUM = 14; const TYPE_SFIXED32 = 15; const TYPE_SFIXED64 = 16; const TYPE_SINT32 = 17; const TYPE_SINT64 = 18; const TYPE_UNKNOWN = -1; static protected $codecs = array(); /** * Setup SPL autoloader for Protobuf library classes * * @static * @return void */ static public function autoload() { spl_autoload_register(function($class){ $prefix = __CLASS__ . '\\'; if (strpos($class, $prefix) === 0) { // Remove vendor from name $class = substr($class, strlen(__NAMESPACE__)+1); // Convert namespace separator to directory ones $class = str_replace('\\', DIRECTORY_SEPARATOR, $class); // Prefix with this file's directory $class = __DIR__ . DIRECTORY_SEPARATOR . $class; include($class . '.php'); return true; } return false; }); } /** * Obtain an instance of the descriptor's registry * * @static * @return Protobuf\Registry */ static public function getRegistry() { static $registry = NULL; if (NULL === $registry) { $registry = new Protobuf\Registry(); } return $registry; } static public function getCodec($codec = null) { if ($codec instanceof Protobuf\CodecInterface) { return $codec; } // Bootstrap the library's default codec if none is available if (!isset(self::$codecs['default'])) { $default = new Protobuf\Codec\Binary(); self::registerCodec('default', $default); self::registerCodec('binary', $default); } if (is_string($codec)) { $codec = strtolower($codec); if (!isset(self::$codecs[$codec])) { throw new Protobuf\Exception('No codec found by name "' . $codec . '"'); } return self::$codecs[$codec]; } return self::getCodec('default'); } static public function setDefaultCodec($codec) { if (is_string($codec)) { $codec = self::getCodec($codec); } if ($codec instanceof Protobuf\CodecInterface) { self::registerCodec('default', $codec); } else { throw new Protobuf\Exception('Codec must implement DrSlump\Protobuf\CodecInterface'); } } static public function registerCodec($name, Protobuf\CodecInterface $codec) { $name = strtolower($name); self::$codecs[$name] = $codec; } static public function unregisterCodec($name) { $name = strtolower($name); if (isset(self::$codecs[$name])) { unset(self::$codecs[$name]); return true; } return false; } /** * Encodes a message using the default codec * * @static * @param \DrSlump\Protobuf\Message $message * @return string */ static public function encode(Protobuf\Message $message) { $codec = self::getCodec(); return $codec->encode($message); } /** * @static * @param String|Message $message * @param String $data * @return \DrSlump\Protobuf\Message */ static public function decode($message, $data) { if (is_string($message)) { $message = '\\' . ltrim($message, '\\'); $message = new $message; } $codec = self::getCodec(); return $codec->decode($message, $data); } } |