Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Ported changes from wikimedia (incl. logical types) and CI integration (
Browse files Browse the repository at this point in the history
  • Loading branch information
tPl0ch authored and mdio committed Feb 25, 2020
1 parent 922a5d9 commit dfdaaa8
Show file tree
Hide file tree
Showing 34 changed files with 3,220 additions and 215 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor/

composer.lock
!bin/php
!build/.gitkeep
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
dist: trusty
sudo: required
language: php

php:
- '7.1'
- '7.2'
- '7.3'

before_script:
- composer self-update
- composer update

script:
- COMPOSER=composer PHP=php make phpunit

after_script:
- make clean

branches:
only:
- master
- '/^ft-.*/'
- '/^\d+\.\d+\.\d+$/'

cache:
directories:
- $HOME/.composer/cache/files
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ARG PHP_VERSION=7.1

FROM php:${PHP_VERSION}-cli-alpine

ARG XDEBUG_VERSION=2.7.0RC1

RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
&& apk add --no-cache --virtual .runtime-deps git libzip-dev \
&& docker-php-ext-install zip \
&& pecl install xdebug-$XDEBUG_VERSION \
&& docker-php-ext-enable xdebug \
&& echo "xdebug.max_nesting_level=15000" >> "$PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini" \
&& echo "xdebug.remote_enable=true" >> "$PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini" \
&& echo "xdebug.remote_host=localhost" >> "$PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini" \
&& echo "xdebug.idekey=PHPSTORM" >> "$PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini" \
&& echo "xdebug.remote_handler=dbgp" >> "$PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini" \
&& echo "xdebug.remote_autostart=1" >> "$PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini" \
&& echo "xdebug.remote_connect_back=0" >> "$PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini" \
&& apk del .build-deps
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# no buildin rules and variables
MAKEFLAGS =+ -rR --warn-undefined-variables

.PHONY: composer-install composer-update examples docker run

COMPOSER ?= bin/composer.phar
COMPOSER_VERSION ?= 1.8.3
PHP ?= bin/php
PHP_VERSION ?= 7.2
XDEBUG_VERSION ?= 2.7.0RC1

export

docker:
docker build \
--build-arg PHP_VERSION=$(PHP_VERSION) \
--build-arg XDEBUG_VERSION=$(XDEBUG_VERSION) \
-t avro-php:$(PHP_VERSION) \
-f Dockerfile \
.

composer-install:
PHP_VERSION=$(PHP_VERSION) $(PHP) $(COMPOSER) install --no-interaction --no-progress --no-suggest --no-scripts

composer-update:
PHP_VERSION=$(PHP_VERSION) $(PHP) $(COMPOSER) update --no-interaction --no-progress --no-suggest --no-scripts

phpunit:
@mkdir -p build/tmp build/share/test/schemas build/build/interop/data
@chmod -R a+w build
PHP_VERSION=$(PHP_VERSION) $(PHP) vendor/bin/phpunit --coverage-text test/AllTests.php

run:
PHP_VERSION=$(PHP_VERSION) $(PHP) $(ARGS)

examples:
PHP_VERSION=$(PHP_VERSION) $(PHP) examples/*

install-phars:
curl https://getcomposer.org/download/$(COMPOSER_VERSION)/composer.phar -o bin/composer.phar -LR -z bin/composer.phar
chmod a+x bin/composer.phar

install: install-phars docker composer-install

clean:
rm -r build/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A library for using [Avro](http://avro.apache.org/) with PHP.

Requirements
============
* PHP >= 5.5
* PHP >= 7.1
* On 32-bit platforms, the [GMP PHP extension](http://php.net/gmp)
* For testing, [PHPUnit](http://www.phpunit.de/)

Expand Down
22 changes: 22 additions & 0 deletions bin/php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env sh

set -e

command -v docker >/dev/null 2>&1 || { echo "docker is required to run this binary. Aborting." >&2; exit 1; }

USER=${USER:-$( id -un )}
GROUP=${GROUP:-$( id -gn )}
COMPOSER_HOME=${COMPOSER_HOME:-${HOME}/.composer}
PHP_VERSION=${PHP_VERSION:-7.1}
DOCKER_OPTS=${DOCKER_OPTS:-'-it'}

exec docker run ${DOCKER_OPTS} --rm \
-u $( id -u ${USER} ):$( id -g ${USER} ) \
-v "${PWD}":"${PWD}" \
-v "${COMPOSER_HOME}":/tmp/composer \
-w ${PWD} \
-e PHP_IDE_CONFIG="serverName=avro-php" \
-e COMPOSER_HOME="/tmp/composer" \
--net=host --sig-proxy=true --pid=host \
--entrypoint="php" \
avro-php:${PHP_VERSION} "${@}"
Empty file added build/.gitkeep
Empty file.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"description": "Composer packaging for vanilla Apache Avro, with fixes",
"license": "Apache-2.0",
"require": {
"php": ">=5.5.0"
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^5.0"
},
"suggest": {
"ext-gmp": "Large integer support for 32-bit platforms."
},
"autoload": {
"classmap": ["lib/"]
Expand Down
12 changes: 9 additions & 3 deletions lib/avro.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
*/
class AvroException extends Exception {}

/**
* Unimplemented feature exception.
* @package Avro
*/
class AvroNotImplementedException extends AvroException {}

/**
* Library-level class for PHP Avro port.
*
Expand Down Expand Up @@ -113,7 +119,7 @@ private static function check_64_bit()
}

/**
* @returns boolean true if the PHP GMP extension is used and false otherwise.
* @return boolean true if the PHP GMP extension is used and false otherwise.
* @internal Requires Avro::check_64_bit() (exposed via Avro::check_platform())
* to have been called to set Avro::$biginteger_mode.
*/
Expand Down Expand Up @@ -161,7 +167,7 @@ private static function set_endianness()
}

/**
* @returns boolean true if the host platform is big endian
* @return boolean true if the host platform is big endian
* and false otherwise.
* @uses self::set_endianness()
*/
Expand All @@ -174,7 +180,7 @@ private static function is_big_endian_platform()
}

/**
* @returns boolean true if the host platform is little endian,
* @return boolean true if the host platform is little endian,
* and false otherwise.
* @uses self::is_bin_endian_platform()
*/
Expand Down
56 changes: 33 additions & 23 deletions lib/avro/data_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ class AvroDataIO
private static $metadata_schema;

/**
* @returns the initial "magic" segment of an Avro container file header.
* @return string the initial "magic" segment of an Avro container file header.
*/
public static function magic() { return ('Obj' . pack('c', self::VERSION)); }

/**
* @returns int count of bytes in the initial "magic" segment of the
* @return int count of bytes in the initial "magic" segment of the
* Avro container file header
*/
public static function magic_size() { return strlen(self::magic()); }


/**
* @returns AvroSchema object of Avro container file metadata.
* @return AvroSchema object of Avro container file metadata.
*/
public static function metadata_schema()
{
Expand All @@ -111,7 +111,7 @@ public static function metadata_schema()
* @param string $file_path file_path of file to open
* @param string $mode one of AvroFile::READ_MODE or AvroFile::WRITE_MODE
* @param string $schema_json JSON of writer's schema
* @returns AvroDataIOWriter instance of AvroDataIOWriter
* @return AvroDataIOWriter instance of AvroDataIOWriter
*
* @throws AvroDataIOException if $writers_schema is not provided
* or if an invalid $mode is given.
Expand All @@ -122,7 +122,6 @@ public static function open_file($file_path, $mode=AvroFile::READ_MODE,
$schema = !is_null($schema_json)
? AvroSchema::parse($schema_json) : null;

$io = false;
switch ($mode)
{
case AvroFile::WRITE_MODE:
Expand All @@ -144,7 +143,7 @@ public static function open_file($file_path, $mode=AvroFile::READ_MODE,
}

/**
* @returns array array of valid codecs
* @return array array of valid codecs
*/
private static function valid_codecs()
{
Expand All @@ -153,7 +152,7 @@ private static function valid_codecs()

/**
* @param string $codec
* @returns boolean true if $codec is a valid codec value and false otherwise
* @return boolean true if $codec is a valid codec value and false otherwise
*/
public static function is_valid_codec($codec)
{
Expand All @@ -163,7 +162,7 @@ public static function is_valid_codec($codec)
/**
* @param AvroIO $io
* @param AvroSchema $schema
* @returns AvroDataIOWriter
* @return AvroDataIOWriter
*/
protected static function open_writer($io, $schema)
{
Expand All @@ -174,7 +173,7 @@ protected static function open_writer($io, $schema)
/**
* @param AvroIO $io
* @param AvroSchema $schema
* @returns AvroDataIOReader
* @return AvroDataIOReader
*/
protected static function open_reader($io, $schema)
{
Expand Down Expand Up @@ -209,12 +208,12 @@ class AvroDataIOReader
/**
* @var string
*/
private $sync_marker;
public $sync_marker;

/**
* @var array object container metadata
*/
private $metadata;
public $metadata;

/**
* @var int count of items in block
Expand Down Expand Up @@ -278,30 +277,28 @@ private function read_header()

/**
* @internal Would be nice to implement data() as an iterator, I think
* @returns \Generator
* @return array of data from object container.
*/
public function data()
{
$data = array();
while (true)
{
if (0 == $this->block_count)
{
if ($this->is_eof()) {
if ($this->is_eof())
break;
}

if ($this->skip_sync()) {
if ($this->is_eof()) {
if ($this->skip_sync())
if ($this->is_eof())
break;
}
}

$this->read_block_header();
}
$data = $this->datum_reader->read($this->decoder);
$data []= $this->datum_reader->read($this->decoder);
$this->block_count -= 1;
yield $data;
}
return $data;
}

/**
Expand All @@ -312,6 +309,10 @@ public function close() { return $this->io->close(); }

/**
* @uses AvroIO::seek()
* @param $offset
* @param $whence
* @return bool
* @throws AvroNotImplementedException
*/
private function seek($offset, $whence)
{
Expand All @@ -320,6 +321,9 @@ private function seek($offset, $whence)

/**
* @uses AvroIO::read()
* @param $len
* @return string
* @throws AvroNotImplementedException
*/
private function read($len) { return $this->io->read($len); }

Expand All @@ -328,6 +332,9 @@ private function read($len) { return $this->io->read($len); }
*/
private function is_eof() { return $this->io->is_eof(); }

/**
* @return bool
*/
private function skip_sync()
{
$proposed_sync_marker = $this->read(AvroDataIO::SYNC_SIZE);
Expand All @@ -342,7 +349,7 @@ private function skip_sync()
/**
* Reads the block header (which includes the count of items in the block
* and the length in bytes of the block)
* @returns int length in bytes of the block.
* @return int length in bytes of the block.
*/
private function read_block_header()
{
Expand All @@ -359,7 +366,7 @@ private function read_block_header()
class AvroDataIOWriter
{
/**
* @returns string a new, unique sync marker.
* @return string a new, unique sync marker.
*/
private static function generate_sync_marker()
{
Expand Down Expand Up @@ -411,6 +418,7 @@ private static function generate_sync_marker()
* @param AvroIO $io
* @param AvroIODatumWriter $datum_writer
* @param AvroSchema $writers_schema
* @throws AvroDataIOException
*/
public function __construct($io, $datum_writer, $writers_schema=null)
{
Expand Down Expand Up @@ -470,7 +478,7 @@ public function close()

/**
* Flushes biffer to AvroIO object container.
* @returns mixed value of $io->flush()
* @return mixed value of $io->flush()
* @see AvroIO::flush()
*/
private function flush()
Expand Down Expand Up @@ -522,13 +530,15 @@ private function write_header()
/**
* @param string $bytes
* @uses AvroIO::write()
* @return int
*/
private function write($bytes) { return $this->io->write($bytes); }

/**
* @param int $offset
* @param int $whence
* @uses AvroIO::seek()
* @return bool
*/
private function seek($offset, $whence)
{
Expand Down
Loading

0 comments on commit dfdaaa8

Please sign in to comment.