3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * Avro library top-level file.
23 * This file in turn includes all files supporting the
24 * Avro implementation.
30 * General Avro exceptions.
33 class AvroException extends Exception {}
36 * Library-level class for PHP Avro port.
38 * Contains library details such as version number and platform checks.
40 * This port is an implementation of the
41 * {@link http://avro.apache.org/docs/1.3.3/spec.html Avro 1.3.3 Specification}
49 * @var string version number of Avro specification to which
50 * this implemenation complies
52 const SPEC_VERSION = '1.3.3';
55 * Constant to enumerate endianness.
59 const BIG_ENDIAN = 0x00;
60 const LITTLE_ENDIAN = 0x01;
64 * Memoized result of self::set_endianness()
65 * @var int self::BIG_ENDIAN or self::LITTLE_ENDIAN
66 * @see self::set_endianness()
68 private static $endianness;
71 * Constant to enumerate biginteger handling mode.
72 * GMP is used, if available, on 32-bit platforms.
74 const PHP_BIGINTEGER_MODE = 0x00;
75 const GMP_BIGINTEGER_MODE = 0x01;
80 * Mode used to handle bigintegers. After Avro::check_64_bit() has been called,
81 * (usually via a call to Avro::check_platform(), set to
82 * self::GMP_BIGINTEGER_MODE on 32-bit platforms that have GMP available,
83 * and to self::PHP_BIGINTEGER_MODE otherwise.
85 private static $biginteger_mode;
88 * Wrapper method to call each required check.
91 public static function check_platform()
94 self::check_little_endian();
98 * Determines if the host platform can encode and decode long integer data.
100 * @throws AvroException if the platform cannot handle long integers.
102 private static function check_64_bit()
104 if (8 != PHP_INT_SIZE)
105 if (extension_loaded('gmp'))
106 self::$biginteger_mode = self::GMP_BIGINTEGER_MODE;
108 throw new AvroException('This platform cannot handle a 64-bit operations. '
109 . 'Please install the GMP PHP extension.');
111 self::$biginteger_mode = self::PHP_BIGINTEGER_MODE;
116 * @returns boolean true if the PHP GMP extension is used and false otherwise.
117 * @internal Requires Avro::check_64_bit() (exposed via Avro::check_platform())
118 * to have been called to set Avro::$biginteger_mode.
120 static function uses_gmp()
122 return (self::GMP_BIGINTEGER_MODE == self::$biginteger_mode);
126 * Determines if the host platform is little endian,
127 * required for processing double and float data.
129 * @throws AvroException if the platform is not little endian.
131 private static function check_little_endian()
133 if (!self::is_little_endian_platform())
134 throw new AvroException('This is not a little-endian platform');
138 * Determines the endianness of the host platform and memoizes
139 * the result to Avro::$endianness.
141 * Based on a similar check perfomed in http://pear.php.net/package/Math_BinaryUtils
143 * @throws AvroException if the endianness cannot be determined.
145 private static function set_endianness()
147 $packed = pack('d', 1);
150 case "\77\360\0\0\0\0\0\0":
151 self::$endianness = self::BIG_ENDIAN;
153 case "\0\0\0\0\0\0\360\77":
154 self::$endianness = self::LITTLE_ENDIAN;
157 throw new AvroException(
158 sprintf('Error determining platform endianness: %s',
159 AvroDebug::hex_string($packed)));
164 * @returns boolean true if the host platform is big endian
165 * and false otherwise.
166 * @uses self::set_endianness()
168 private static function is_big_endian_platform()
170 if (is_null(self::$endianness))
171 self::set_endianness();
173 return (self::BIG_ENDIAN == self::$endianness);
177 * @returns boolean true if the host platform is little endian,
178 * and false otherwise.
179 * @uses self::is_bin_endian_platform()
181 private static function is_little_endian_platform()
183 return !self::is_big_endian_platform();
188 require_once('avro/util.php');
189 require_once('avro/debug.php');
190 require_once('avro/schema.php');
191 require_once('avro/io.php');
192 require_once('avro/gmp.php');
193 require_once('avro/datum.php');
194 require_once('avro/data_file.php');
195 require_once('avro/protocol.php');