google-api-php-client-services icon indicating copy to clipboard operation
google-api-php-client-services copied to clipboard

Is it possible to migrate this to PSR-4

Open steffdimitrov opened this issue 8 years ago • 9 comments

I know that you guys are using some tool like swagger for code generator, but is it possible to create a new tag/version with psr-4 autoloader. It is much more easier and beautiful for reading when you have code like:

<?php
use Google\Service\CloudTalentSolution;

class Service 
{
    private function someMethod()
    {
        $talentClient = new CloudTalentSolution($client);
    }
}

steffdimitrov avatar Sep 11 '18 06:09 steffdimitrov

Any status for this @mattwhisenhunt or @JustinBeckwith? I'd love to have this since it will make our code cleaner.

chez14 avatar May 18 '19 14:05 chez14

It remains on our agenda, but unfortunately I don't have an ETA for it yet.

jdpedrie avatar Jun 07 '19 15:06 jdpedrie

Migrating to PSR-4 will fix another problem -- huge autoload files:

$ cat >composer.json <<EOF
> {
>   "name": "test/test",
>   "type": "project",
>   "require": { "google/apiclient": "^2.2" },
>   "config": {
>     "optimize-autoloader": true,
>     "preferred-install": "dist"
>   }
> }
> EOF

$ composer install --no-ansi --no-interaction --no-progress --no-suggest --no-dev
Loading composer repositories with package information
Updating dependencies
Package operations: 13 installs, 0 updates, 0 removals
  - Installing ralouphie/getallheaders (3.0.3): Downloading (100%)
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.6.1): Downloading (100%)
  - Installing guzzlehttp/promises (v1.3.1): Loading from cache
  - Installing guzzlehttp/guzzle (6.3.3): Loading from cache
  - Installing phpseclib/phpseclib (2.0.21): Downloading (100%)
  - Installing psr/log (1.1.0): Loading from cache
  - Installing monolog/monolog (1.24.0): Loading from cache
  - Installing firebase/php-jwt (v5.0.0): Loading from cache
  - Installing google/apiclient-services (v0.111): Downloading (100%)
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing google/auth (v1.5.2): Downloading (100%)
  - Installing google/apiclient (v2.2.4): Downloading (100%)
Writing lock file
Generating optimized autoload files

$ du -sch vendor/google/apiclient-services/src/Google/Service/* | grep -v '.*\.php' | sort -h -k1,1 | tail
744K    vendor/google/apiclient-services/src/Google/Service/Dataflow
756K    vendor/google/apiclient-services/src/Google/Service/CloudSearch
904K    vendor/google/apiclient-services/src/Google/Service/DLP
956K    vendor/google/apiclient-services/src/Google/Service/Sheets
1.1M    vendor/google/apiclient-services/src/Google/Service/YouTube
1.3M    vendor/google/apiclient-services/src/Google/Service/ShoppingContent
1.6M    vendor/google/apiclient-services/src/Google/Service/Dfareporting
1.7M    vendor/google/apiclient-services/src/Google/Service/Vision
3.7M    vendor/google/apiclient-services/src/Google/Service/Compute
49M     total

$ du -sch vendor/composer/* | sort -h -k1,1
1.0K    vendor/composer/autoload_namespaces.php
4.0K    vendor/composer/autoload_files.php
4.0K    vendor/composer/autoload_psr4.php
4.0K    vendor/composer/autoload_real.php
4.0K    vendor/composer/LICENSE
16K     vendor/composer/ClassLoader.php
28K     vendor/composer/installed.json
1.5M    vendor/composer/autoload_classmap.php
1.6M    vendor/composer/autoload_static.php
3.2M    total

These 3.2Mb of autoload files are loaded into memory on every processed request despite we need only one service.

vvolodko avatar Aug 30 '19 20:08 vvolodko

I would also really like to see this. More and more frameworks are moving to automated PSR-4 loading and the fact that this library has no namespace of it's own is silly.

evansharp avatar Jan 27 '20 07:01 evansharp

Please add PSR-4 support, not only for making our code prettier but making it faster

marcorivm avatar Sep 15 '20 16:09 marcorivm

The Client is now PSR-4 compliant. Migrating this lib to PSR-4 would be great for homogeneity of the code...

$googleClient = new Google\Client();
$youtubeService = new \Google_Service_YouTube($googleClient); // Curent
$youtubeService = new Google\YouTube\Service($googleClient); // PSR-4

https://github.com/googleapis/google-api-php-client/pull/1937

ThibaultVlacich avatar Oct 23 '20 22:10 ThibaultVlacich

2.5 years later and this is still open? Is this still on your agenda?

ls-justin-bull avatar Mar 05 '21 20:03 ls-justin-bull

I do hope someone is working on it, since Composer 2 will not autoload PSR-0 style classes anymore, hence possibly breaking library usage on a modern web application...

ZeroThe2nd avatar Apr 23 '21 07:04 ZeroThe2nd

This has been merged in #363 and tagged in v0.200.0! You can receive this update immediately by running composer update!

bshaffer avatar Jun 18 '21 19:06 bshaffer