GithubHelp home page GithubHelp logo

jbboehr / php-psr Goto Github PK

View Code? Open in Web Editor NEW
226.0 21.0 31.0 642 KB

PHP extension providing the accepted PSR interfaces

License: Other

C 55.20% M4 0.73% JavaScript 0.49% Nix 1.94% PHP 41.65%
psr c php php-extension php-fig

php-psr's Introduction

php-psr

GitHub Build Status GitHub Windows Build Status Appveyor Build Status Coverage Status License

This PHP extension provides the interfaces from the PSR standards as established by the PHP-FIG group. You can use interfaces provided by this extension in another extension easily - see this example.

As of v1.2 of the extension, classes are defined in the PsrExt namespace and aliased into the Psr namespace.

Interfaces

PSR Reference
PSR-3 psr/log *
PSR-6 psr/cache
PSR-7 psr/http-message
PSR-11 psr/container
PSR-13 psr/link
PSR-14 psr/event-dispatcher
PSR-15 psr/http-server-handler
PSR-15 psr/http-server-middleware
PSR-16 psr/simple-cache
PSR-17 psr/http-factory
PSR-18 psr/http-client

Installation

Linux / macOS

Prerequisite packages are:

  • PHP development headers and tools, php >= 7.3
  • gcc >= 4.4 | clang >= 3.x | vc >= 11
  • GNU make >= 3.81
  • automake
  • autoconf

You will need the PHP development headers. If PHP was manually installed, these should be available by default. Otherwise, you will need to fetch them from a repository.

git clone https://github.com/jbboehr/php-psr.git
cd php-psr
phpize
./configure
make
make test
sudo make install

If you have specific PHP versions running:

git clone https://github.com/jbboehr/php-psr.git
cd php-psr
/usr/local/bin/phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make test
sudo make install

Add the extension to your php.ini:

echo extension=psr.so | tee -a /path/to/your/php.ini

Finally, restart the web server.

PECL / Windows

You may also be able to install this extension via PECL:

pecl install psr

or by downloading a DLL from PECL or windows.php.net and placing it in the appropriate directory.

Nix / NixOS

nix-env -i -f https://github.com/jbboehr/php-psr/archive/master.tar.gz

with a custom version of PHP:

nix-env -i -f https://github.com/jbboehr/php-psr/archive/master.tar.gz --arg php '(import <nixpkgs> {}).php71'

or, in a .nix file:

(import <nixpkgs> {}).callPackage (import (fetchTarball {
  url = https://github.com/jbboehr/php-psr/archive/v1.2.0.tar.gz;
  sha256 = "1c4jc09d46ac43kkckil8l76is973czwp0g6vhpnv5105l7z3zgi";
})) {}

Using with composer

In your project, you can prevent the installation of the unnecessary composer packages provided by this extension by adding the following to your composer.json. You will need to make sure the extension is installed and enabled in your PHP configuration. You may also want to consider leaving them installed to provide stubs to your IDE. This configuration is not suitable for a library published to packagist.

Note: LoggerInterfaceTest and TestLogger from psr/log are not implemented. If you use these classes, you should keep psr/log installed (discussion).

{
    "name": "sample/app",
    "require": {
        "ext-psr": "*"
    },
    "provide": {
        "psr/log": "1.0.0",
        "psr/cache": "1.0.0",
        "psr/http-message": "1.0.0",
        "psr/container": "1.1.1",
        "psr/link": "1.0.0",
        "psr/event-dispatcher": "1.0.0",
        "psr/http-server-handler": "1.0.0",
        "psr/http-server-middleware": "1.0.0",
        "psr/simple-cache": "1.0.0",
        "psr/http-factory": "1.0.0",
        "psr/http-client": "1.0.0"
    },
}

This will use this PHP extension instead of downloading from packagist.

Credits

License

This project is open source software licensed under the Simplified BSD License. See the LICENSE.md file for more information.

PSR Interfaces: Copyright (c) 2012-present PHP Framework Interoperability Group.

php-psr's People

Contributors

derrabus avatar flyinghail avatar jasny avatar jbboehr avatar jeckerson avatar mhf-ir avatar remicollet avatar sergeyklay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-psr's Issues

Failed to install with PHP 7.4

Hi there,

I have trying to install PSR (from source) on PHP 7.4 (from Ondrej repo), but got this error

$ php7.4 --ri psr
PHP Warning:  PHP Startup: psr: Unable to initialize module
Module compiled with module API=20180731
PHP    compiled with module API=20190902
These options need to match
 in Unknown on line 0
PHP Warning:  Cannot load module 'phalcon' because required module 'psr' is not loaded in Unknown on line 0
Extension 'psr' not present.

My system

$ uname -a
Linux dev.eslabs.id 4.15.0-72-generic #81-Ubuntu SMP Tue Nov 26 12:20:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
$ /usr/bin/phpize7.4 --version
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902
$ /usr/bin/php-config7.4 --version
7.4.1

any solutions?

thanks

PSR v7.0 fails with "symbol lookup error"

System

  • OpenSuse 15.1
  • PHP 7.8 nts, no debug, Zend OPcache v7.3.8 enabled

Problem

After compiling master or v7.0 (from Github) I get the following errror:

php -v
php: symbol lookup error: /usr/local/lib64/extensions/no-debug-non-zts-20180731/psr.so: undefined symbol: zm_startup_psr_event_dispatcher

PHP gets unusable.
Btw: version 0.6.1 works fine!

This extension causes major compatibly issues

I do appreciate all OS contributions and I really don't want to discourage others work. But I need to inform you that there is a huge problem when people are using this extension.

Since the code is loaded with PHP, it will avoid all composer version negotiation. Which means a user will always have psr/container:X and when they download a package that only supports psr/container:Y composer will not complain but the user will get an error at runtime.

I see that you have a section in the readme to inform users about this problem but it has two problems:

  1. Not all users add that to their composer.json
  2. It is not 100% correct.

I see a lot of confused users reporting issues they get after installing libraries that I maintain.

Example:

Fatal error: Declaration of Symfony\Component\DependencyInjection\ServiceLocator::has(string $id) must be compatible with Psr\Container\ContainerInterface::has($id) in vendor/symfony/dependency-injection/ServiceLocator.php on line 46

I strongly suggest this should be the top priority to fix. Especially since the problem will get way worse with #87.

Add example on how to use logger traits in an extension

e.g.

    zend_class_entry ce;

    INIT_NS_CLASS_ENTRY(ce, GUZZLE_NS, "Client", guzzle_client_method);

    guzzle_client_ce = zend_register_internal_class(&ce);

    zend_do_implement_trait(guzzle_client_ce, PsrLogLoggerTrait_ce_ptr);
    zend_do_bind_traits(guzzle_client_ce); // zend_add_trait_method --> zend_arena_alloc error 

Failed tests with PHP 5.6

Build OK.

TEST 8/43 [tests/PsrHttpClientClientExceptionInterface.phpt]
========DIFF========
001+ Warning: class_implements(): Class Psr\Http\Client\ClientExceptionInterface does not exist in /builddir/build/BUILD/php56-php-pecl-psr-0.6.0/NTS/tests/PsrHttpClientClientExceptionInterface.php on line 3
002+ bool(false)
003+ 
004+ Fatal error: Interface 'Psr\Http\Client\ClientExceptionInterface' not found in /builddir/build/BUILD/php56-php-pecl-psr-0.6.0/NTS/tests/PsrHttpClientClientExceptionInterface.php on line 4
001- array(1) {
002-   ["Throwable"]=>
003-   string(9) "Throwable"
004- }
005- bool(true)
006- bool(true)
007- bool(true)
008- string(4) "test"
========DONE========
FAIL Psr\Http\Client\ClientExceptionInterface [tests/PsrHttpClientClientExceptionInterface.phpt] 

TEST 9/43 [tests/PsrHttpClientClientInterface.phpt]
========DIFF========
001+ Parse error: syntax error, unexpected ':', expecting ';' or '{' in /builddir/build/BUILD/php56-php-pecl-psr-0.6.0/NTS/tests/SampleClient.inc on line 9
001- bool(true)
002- array(1) {
003-   ["Psr\Http\Client\ClientInterface"]=>
004-   string(31) "Psr\Http\Client\ClientInterface"
005- }
006- string(25) "SampleClient::sendRequest"
007- object(SampleRequest)#%d (0) {
008- }
009- bool(true)
========DONE========
FAIL Psr\Http\Client\ClientInterface [tests/PsrHttpClientClientInterface.phpt] 

TEST 10/43 [tests/PsrHttpClientNetworkExceptionInterface.phpt]
========DIFF========
001+ Parse error: syntax error, unexpected ':', expecting ';' or '{' in /builddir/build/BUILD/php56-php-pecl-psr-0.6.0/NTS/tests/PsrHttpClientNetworkExceptionInterface.php on line 8
001- array(2) {
002-   ["Psr\Http\Client\ClientExceptionInterface"]=>
003-   string(40) "Psr\Http\Client\ClientExceptionInterface"
004-   ["Throwable"]=>
005-   string(9) "Throwable"
006- }
007- bool(true)
008- bool(true)
009- bool(true)
010- bool(true)
011- string(4) "test"
012- string(23) "MyException::getRequest"
013- object(SampleRequest)#%d (0) {
014- }
========DONE========
FAIL Psr\Http\Client\NetworkExceptionInterface [tests/PsrHttpClientNetworkExceptionInterface.phpt] 

TEST 11/43 [tests/PsrHttpClientRequestExceptionInterface.phpt]
========DIFF========
001+ Parse error: syntax error, unexpected ':', expecting ';' or '{' in /builddir/build/BUILD/php56-php-pecl-psr-0.6.0/NTS/tests/PsrHttpClientRequestExceptionInterface.php on line 8
001- array(2) {
002-   ["Psr\Http\Client\ClientExceptionInterface"]=>
003-   string(40) "Psr\Http\Client\ClientExceptionInterface"
004-   ["Throwable"]=>
005-   string(9) "Throwable"
006- }
007- bool(true)
008- bool(true)
009- bool(true)
010- bool(true)
011- string(4) "test"
012- string(23) "MyException::getRequest"
013- object(SampleRequest)#%d (0) {
014- }
========DONE========
FAIL Psr\Http\Client\RequestExceptionInterface [tests/PsrHttpClientRequestExceptionInterface.phpt] 

Auto detect .ini dir

Hey,
I'm from PHP-FIG group.

I'm working on phalcon project with @sergeyklay.
I'm integrating PSR-11 ContainerInterface .. I created this bash script to install psr.so ..

My question is .. I don't like how path to conf.d is hard coded .. do you have a good way to auto-detect this ?

#!/usr/bin/env bash
set -ex

apt-get update -y
apt-get install -y nano tree git gnupg gcc make re2c autoconf automake

mkdir -p /tmp/psr-ext-setup
cd /tmp/psr-ext-setup

rm -rf /tmp/psr-ext-setup/php-psr
git clone https://github.com/jbboehr/php-psr.git
cd php-psr
phpize
./configure
make
make test
make install
rm -rf /var/lib/apt/lists/*

echo extension=psr.so | tee -a /etc/php/7.1/cli/conf.d/psr.ini
php -m | grep psr


ServerRequestInterface not implements RequestInterface methods?

Hello, I am using the extension to use the PSR interfaces in a separate Zephir extension.

My method receives as a parameter a ServerRequestInterface and when invoking the method getUri() reports: Class 'Psr\Http\Message\ServerRequestInterface' does not implement method: 'getUri'

Example:

public function handle(<ServerRequestInterface> request) -> <ResponseInterface> {
    // error:
    var_dump(request->getUri()->getPath());

    // ok:
    var requestBase;
    let requestBase = new RequestInterface();
    let requestBase = request;

    var_dump(requestBase->getUri()->getPath());
}

To solve this I must create a new variable of type RequestInterface to "convert" the type of object.

Do you know what may be happening?

Thank you

New release with PHP 8 support?

Hi @jbboehr and others,

PHP 8.0.0 is getting really close and it looks like this extension supports it already.

All that's missing is a Git tag, and a release to PECL :)

Would be super if that could be done anytime soon so that e.g. PaaSes have a stable release version to provide to customers.

Thanks!

1.0?

May I propose bumping the next version (or making a release without changes) to 1.0.0?

I'm asking this mainly due to SemVer's and Composers well-defined differences between pre-1.0 and all other version strings for comparison purposes (as ^0.7.0 means >= 0.7.0 <0.8.0), so it's likely to make life a bit easier long-term for the interoperability of components that want to rely on the extension.

Not compatible with PHP <7.3

I'm compiling php-psr on a fresh CentOS 8 installation, which at the time of writing comes with PHP 7.2.24, and it won't compile with that version due to an incompatibility with the zend API header:

In file included from /home/gerben/php-psr/psr_cache.c:15:
/home/gerben/php-psr/psr_private.h: In function ‘php_psr_register_interface’:
/home/gerben/php-psr/psr_private.h:17:5: error: too many arguments to function ‘zend_register_class_alias_ex’
     zend_register_class_alias_ex(class_alias, class_alias_len, class_entry, 1);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/php/main/php.h:39,
                 from /home/gerben/php-psr/psr_cache.c:6:
/usr/include/php/Zend/zend_API.h:307:14: note: declared here
 ZEND_API int zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce);
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

So psr_private.h calls zend_register_class_alias_ex with 4 arguments:

zend_register_class_alias_ex(class_alias, class_alias_len, class_entry, 1);

But zend_API.h expects only 3 arguments:

ZEND_API int zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce);

The last version where 3 arguments where still expected was 7.2.24:
https://github.com/php/php-src/blob/PHP-7.2.24/Zend/zend_API.h#L307

It expects 4 arguments since PHP version 7.3:
https://github.com/php/php-src/blob/PHP-7.3/Zend/zend_API.h#L289

Can you please update the php-psr README.md requirements to something along the lines of "PHP >= 7.3 development headers and tools"?

Compatibility with newer psr libraries

Currently there is a 2.0 out of psr/container and psr/link which require PHP 8.0 and are not compatible with the libraries we test against. We should review other PSR repos for newer versions develop an upgrade plan.

See #84

Improper type passed to log

Omitting the second argument to the functions in AbstractLogger appear to not forward the argument on as an array.

php-7.2 build psr.so extension error

Installing the PSR extension for PHP 7.2 can sometimes be challenging, especially if you encounter errors during the build or installation process. Here is my experience: I followed the steps you provided to install the PSR extension for PHP 7.2, but encountered an error during the build process:

# /usr/local/php72/bin/phpize
# ./configure --with-php-config=/usr/local/php72/bin/php-config
...
# make
/bin/sh /usr/local/src/php-psr/libtool --mode=compile cc  -I. -I/usr/local/src/php-psr -DPHP_ATOM_INC -I/usr/local/src/php-psr/include -I/usr/local/src/php-psr/main -I/usr/local/src/php-psr -I/usr/local/php72/include/php -I/usr/local/php72/include/php/main -I/usr/local/php72/include/php/TSRM -I/usr/local/php72/include/php/Zend -I/usr/local/php72/include/php/ext -I/usr/local/php72/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /usr/local/src/php-psr/psr_cache.c -o psr_cache.lo 
 cc -I. -I/usr/local/src/php-psr -DPHP_ATOM_INC -I/usr/local/src/php-psr/include -I/usr/local/src/php-psr/main -I/usr/local/src/php-psr -I/usr/local/php72/include/php -I/usr/local/php72/include/php/main -I/usr/local/php72/include/php/TSRM -I/usr/local/php72/include/php/Zend -I/usr/local/php72/include/php/ext -I/usr/local/php72/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /usr/local/src/php-psr/psr_cache.c  -fPIC -DPIC -o .libs/psr_cache.o
In file included from /usr/local/src/php-psr/psr_cache.c:15:
/usr/local/src/php-psr/psr_private.h: In function ‘php_psr_register_interface’:
/usr/local/src/php-psr/psr_private.h:17: error: too many arguments to function ‘zend_register_class_alias_ex’
In file included from /usr/local/src/php-psr/psr_cache.c:15:
/usr/local/src/php-psr/psr_private.h: In function ‘php_psr_register_class’:
/usr/local/src/php-psr/psr_private.h:53: error: too many arguments to function ‘zend_register_class_alias_ex’
make: *** [psr_cache.lo] Error 1

Incorrect default value for methods in \Psr\Log\LoggerInterface

Originally issue came from the Magento 2: magento/magento2#30575

We faced an issue with auto-generated proxy code based on the \Psr\Log\LoggerInterface.
Investigation shown that actually issue was caused by psr php extension - seems like default value isn't specified, while on original LoggerInterface we do have optional context argument wit default value - empty array.

Tested on php 7.3 and php 7.4, both versions having the same issue.

I was able to reproduce this issue even on a simple file (checked on emergency method, but issue the same for others):

<?php
include "./vendor/autoload.php";

$reflectionMethod = new \ReflectionMethod(\Psr\Log\LoggerInterface::class, "emergency");
$reflectionParameter = $reflectionMethod->getParameters()[1];
var_dump($reflectionParameter->isOptional());
var_dump($reflectionParameter->isDefaultValueAvailable());
var_dump($reflectionParameter->getDefaultValue());

and psr/log installed through composer.

Expected result

Result without psr extension

app@3bedbfa9e5db:~/html/pub$ php index.php 
bool(true)
bool(true)
array(0) {
}

Actual result

Result with psr extension

app@3bedbfa9e5db:~/html/pub$ php index.php 
bool(true)
bool(false)

Fatal error: Uncaught ReflectionException: Cannot determine default value for internal functions in /var/www/html/pub/index.php:8
Stack trace:
#0 /var/www/html/pub/index.php(8): ReflectionParameter->getDefaultValue()
#1 {main}
  thrown in /var/www/html/pub/index.php on line 8

Compatibility with psr/container 1.1

This extension currently breaks libraries that provide an implementation of psr/container 1.1, like Pimple 3.4 or Symfony DI 5.3.

Example

composer.json

{
    "require": {
        "psr/container": "^1.1"
    }
}

MyContainer.php

<?php

use Psr\Container\ContainerInterface;

require __DIR__.'/vendor/autoload.php';

class MyContainer implements ContainerInterface
{
    public function has(string $id)
    {
        return false;
    }

    public function get(string $id)
    {
        throw new class extends \Exception implements NotFoundExceptionInterface {};
    }
}

Output

Fatal error: Declaration of MyContainer::get(string $id) must be compatible with Psr\Container\ContainerInterface::get($id) in /path/to/MyContainer.php on line 14

The script is executed correctly if I disable the psr extension.

Release v0.3.0, v0.4.0

I think we're way overdue for a release of 0.3.0. I think it should be safe to include #31 with my PHP5 compatibility changes.

@sergeyklay Is there anything you want in before release?

Thank you from Phalcon

Hello @jbboehr,

It's not a real issue. I just wanted to say you thanks and show you a page which says that we are dependent on php-psr extension: https://docs.phalconphp.com/4.0/en/installation

I think Phalcon is the first so large and public project requiring php-psr extension.
Thank you helping us make Phalcon better.

P.S. Feel free to close this issue after reading

Declaration of `Psr\Http\Message\StreamInterface::__toString()` must be compatible with `Stringable::__toString()`

Since PHP8.1 it is more stricter.

PHP_PSR_BEGIN_ARG_INFO(HttpMessage, StreamInterface, __toString, 0)

Change from:

PHP_PSR_BEGIN_ARG_INFO(HttpMessage, StreamInterface, __toString, 0)

To:

#if PHP_VERSION_ID >= 80000
PHP_PSR_BEGIN_ARG_WITH_RETURN_TYPE_INFO(HttpMessage, StreamInterface, __toString, 0, IS_STRING, 0)
#else
PHP_PSR_BEGIN_ARG_INFO(HttpMessage, StreamInterface, __toString, 0)
#endif

Also this one:

PHP Fatal error: Declaration of Psr\Http\Message\UriInterface::__toString() must be compatible with Stringable::__toString(): string

Found in https://github.com/zephir-lang/zephir/runs/4132222740?check_suite_focus=true#step:11:12

Clarification on the purpose of this extension.

I have a question, and I apologise in advance if GitHub issues is not the right channel for asking this.

What exactly is the purpose of this extension? The project read me says

You can use interfaces provided by this extension in another extension easily

Does that mean that the extension is only for use with other extensions that depend on this? Is there any advantage in standalone use of this extension instead of the standard composer package offered by PHP-FIG?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.