expectedbehavior / php-docraptor

PHP consumer for the DocRaptor.com API
ISC License
7 stars 1 forks source link

ApiWrapper not loading for PSR-4 #32

Closed viccherubini closed 9 years ago

viccherubini commented 9 years ago

I'm using Composer version 1.0-dev 69210d5bc130f8cc9f96f99582a041254d7b9833 2015-05-25 13:29:26 and the ApiWrapper class can not be found using the autoloader.

First, I downloaded Composer from getcomposer.org and then ran the following command:

php composer.phar require "expectedbehavior/php-docraptor" "1.0.0"

This created a composer.json file that looks like this:

{
    "require": {
        "expectedbehavior/php-docraptor": "1.0.0"
    }
}

I copied the sample code from the README into this file:

<?php

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

$apiKey = 'MY_API_KEY';

$docRaptor = new \DocRaptor\ApiWrapper($apiKey);
$docRaptor->setDocumentContent('<h1>Test</h1>')
    ->setDocumentType('pdf')
    ->setTest(true)
    ->setName('test.pdf');

$file = $docRaptor->fetchDocument();
file_put_contents('test.pdf', $file);

When running this, I get the following error:

PHP Fatal error:  Class 'DocRaptor\ApiWrapper' not found in example.php on line 7

If I go into the vendor/expectedbehavior/php-docraptor/src/ directory and rename DocRaptor.php to ApiWrapper.php it works fine.

Am I missing something with my Composer file, or does PSR-4 expect the class and file to have the same name?

markushausammann commented 9 years ago

Always run composer self-update before you do anything else. What PHP Version are you on? This shouldn't happen, you shouldn't need to rename any files, PSR-4 doesn't require the filename to be anything specific.

viccherubini commented 9 years ago

I'm using the latest version of Composer. And I tested on PHP 5.5.20 on OSX and PHP 5.6.5 on Ubuntu 12.04 64bit.

markushausammann commented 9 years ago

This is very weird. I can reproduce it but it doesn't make sense. It should work. Need to investigate. Maybe it's even a composer bug which I would normally strongly doubt.

janxious commented 9 years ago

I have read a bit about psr-4 and it seems like the library is slightly in the wrong. I believe the rename is the correct thing to do. This is the most clear article I have found on the subject: http://www.sitepoint.com/battle-autoloaders-psr-0-vs-psr-4/

viccherubini commented 9 years ago

My knowledge of the PSR's is pretty limited but I think the class name and file name need to match.

markushausammann commented 9 years ago

I guess I was wrong about psr-4. Only strange that it worked for me so far that way. I'll try to find some time to find out why.

janxious commented 9 years ago

@leftnode @markushausammann I will try to get a PR up for this this week if you don't beat me to it.

janxious commented 9 years ago

PR up!

janxious commented 9 years ago

@leftnode Can you try Version 1.1.0 and let me know if you run into the same problems?

markushausammann commented 9 years ago

@janxious you'll need to tag a release before that, just in case.

janxious commented 9 years ago

Derp. That is done, and also added one for 1.0.1.

viccherubini commented 9 years ago

May have to do a new release, this one is missing a few things:

PHP Notice:  Undefined variable: config in /Users/vic/Development/utilities/docs/vendor/expectedbehavior/php-docraptor/src/HttpClient.php on line 46
PHP Fatal error:  Call to a member function getReportUserAgent() on a non-object in /Users/vic/Development/utilities/docs/vendor/expectedbehavior/php-docraptor/src/HttpClient.php on line 46

Updated the call to $config on line 46 to be $this->config and updated the call to userAgent() on line 47 to be $this->userAgent() (both in HttpClient.php). Everything else worked from there.

janxious commented 9 years ago

Was running into issues where our tests are not actually testing the HTTPClient directly. There is a new version 1.1.1 that should resolve the issue. I have also deleted the 1.1.0 tag since that was no good.

viccherubini commented 9 years ago

Just pulled in 1.1.1 and was able to build a document correctly. Thanks!

janxious commented 9 years ago

:+1: Good to hear! I really appreciate your patience, and I am sorry for taking so much of your time.

viccherubini commented 9 years ago

Hah, no problem at all - glad it got worked out.