Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Autoloader (i.e. failed requires) - Read before file a bug report for failed requires! #49

Closed
eyecatchup opened this issue Nov 5, 2013 · 18 comments
Assignees
Milestone

Comments

@eyecatchup
Copy link
Owner

LAST EDITED 2013-12-16

Okay. First, there were some cross-platform path name seperator issues ("" instead of "/"), which resulted in many users experienced failed requires. On the other hand, there were some composer users having issues with my custom PSR-0 Autoloader class.

Because these issues existed for some time and I still had no time to fix it myself, I merged #41 and #42. These commits (thanks to @francisbesset) fix both issues by using the PHP constant DIRECTORY_SEPARATOR as a path name seperator and replacing the custom Autoloader class by composer's.

However, this breaks autoloading without composer for the current master branch. I will merge back the custom PSR-0 Autoloader class for alternative use with the upcoming 2.5.3 release.

If you have issues with failed requires, for the moment the best way is to use composer to install SEOstats (as referenced here). The alternative way, downloading the Zip-File from Github does not work! If there's no way for you to work with composer, you can still download the Zip-File of the Dev-Version of 2.5.3 here: https://github.com/eyecatchup/SEOstats/archive/dev-253.zip . This still includes the "old" custom Autoloader class for you to use the examples out of the box. However, you need to change the back slashes ("") - used in the example files to require the autoloader class - with the DIRECTORY_SEPARATOR constant (see this comment for more detailed instructions).

@florentcm
Copy link
Collaborator

Is the composer supposed to create the Autoload.php file in Common directory during installation ?

@drvipinlalt
Copy link

I got an error, i don't find autoload.php anywhere

Link is http://pagerank.co.in/seostats/example/get-alexa-graphs.php

Warning: require_once(/home3/shopkera/public_html/www.pagerank.co.in/seostats/example/../vendor/autoload.php) [function.require-once]: failed to open stream: No such file or directory in /home3/shopkera/public_html/www.pagerank.co.in/seostats/example/get-alexa-graphs.php on line 13

Fatal error: require_once() [function.require]: Failed opening required '/home3/shopkera/public_html/www.pagerank.co.in/seostats/example/../vendor/autoload.php' (include_path='.:/opt/php53/lib/php') in /home3/shopkera/public_html/www.pagerank.co.in/seostats/example/get-alexa-graphs.php on line 13

@eyecatchup
Copy link
Owner Author

@knotnow

Is the composer supposed to create the Autoload.php file in Common directory during installation ?

Nope, it's not. Composer creates an autoload.php in ./vendor/autoload.php - relative to the path where you run the install command. This autoloader is actually just a stub to ./vendor/composer/autoload_real.php, which finally includes and registers the custom class map and namespace(s) and in turn makes the old Common/AutoLoader.php (that would live in ./vendor/seostats/seostats/SEOstats/Common/AutoLoader.php now) obsolete.

Just include composer's autoloader and you're ready to go. Checkout the following:

As you can see, the example files are still working almost out of the box when placed in composer's SEOstats install path. There was just a wrong require path to composer's ./vendor/autoload.php in the example files (hey, it wasn't me ;)).

C:\xampp\htdocs\seostats-master-20131203>diff get-google-pagerank.php vendor\seostats\seostats\example\get-google-pagerank.php
13c13
< require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
---
> require_once __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php';

I'll push a change to fix the wrong paths in a few minutes.

@eyecatchup
Copy link
Owner Author

@drvipinlalt

I got an error, i don't find autoload.php anywhere
Failed opening required '/home3/shopkera/public_html/www.pagerank.co.in/seostats/example/../vendor/autoload.php'

Well, looking at the paths, you did obviously not installed SEOstats via composer, correct? But http://pagerank.co.in/seostats/composer.json shows that you use the current master branch code (version 2.5.2).

Quote from the first post in this thread: "The alternative way, downloading the Zip-File from Github does not work!"

I'm sorry if I wasn't clear enough, but you must install SEOstats via composer right now. Or, use the Dev-Version of 2.5.3 here: https://github.com/eyecatchup/SEOstats/archive/dev-253.zip

@eyecatchup
Copy link
Owner Author

Fix commited. Also added some more info to the example files for those who download the master zip.

@halbesma
Copy link

Can someone please explain in a bit more detail what this means: "However, you may need to change the forward slashes ("") with the DIRECTORY_SEPARATOR constant."

@eyecatchup
Copy link
Owner Author

@halbesma

Can someone please explain in a bit more detail what this means: "However, you may need to change the forward slashes ("\") with the DIRECTORY_SEPARATOR constant."

Well, first of there's a small mistake - it should read backslash.. Anyway, I was trying to say the following:

Even though the Zip-File of the Dev-Version of 2.5.3 (https://github.com/eyecatchup/SEOstats/archive/dev-253.zip) contains the custom Autoloader class (which is missing in the current master branch), you still need to change the path separators.

The example files (in 2.5.3) contain the following line to specify the path to the bootstrap.php file:

// Bootstrap the library / register autoloader
require_once (__DIR__ . '\..') . '\SEOstats\bootstrap.php';

Backslashes work fine on Windows systems, but don't work on *nix. So, unless you work on a Windows machine, you'll need to change these back slashes ("") to forward slashes ("/"):

// Bootstrap the library / register autoloader
require_once (__DIR__ . '/..') . '/SEOstats/bootstrap.php';

Or - even better - replace the backslashes with PHP's DIRECTORY_SEPARATOR constant (that way it is cross-platform safe):

// Bootstrap the library / register autoloader
require_once (__DIR__ . DIRECTORY_SEPARATOR . '..') . DIRECTORY_SEPARATOR . 'SEOstats' . DIRECTORY_SEPARATOR . 'bootstrap.php';

@halbesma
Copy link

@eyecatchup

Made the changes to the examples as per your post. However I get a 500 error. In the error log it gave me the following:

[16-Dec-2013 15:29:57 America/New_York] PHP Fatal error: Class 'SEOstats\SEOstats' not found in /home/dpuqsevb/public_html/domain/seo/example/get-alexa-graphs.php on line 21

I changed change this line in Autoloader.php:

protected $namespace = '';

to

protected $namespace = '\';

Then I got the following 500 error:

[16-Dec-2013 15:41:10 America/New_York] PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/dpuqsevb/public_html/domain/seo/SEOstats/Common/AutoLoader.php on line 33

I changed all the ' to " in autoloader.php (except for line 18 and 23).
I then get this error:

PHP Parse error: syntax error, unexpected T_NS_SEPARATOR, expecting ',' or ';' in /home/dpuqsevb/public_html/domain/seo/SEOstats/Common/AutoLoader.php on line 23

It refers to this line:

protected $path = '';

@eyecatchup
Copy link
Owner Author

@halbesma thanks for the detailed feedback. checking.. what's your os, server & php version?

@halbesma
Copy link

@eyecatchup
Linux and PHP 5.3.27
Cheers

Also had the same error using another Linux server with PHP 5.4.22

@eyecatchup
Copy link
Owner Author

With the changes I made today, I can now install SEOstats via composer or use the zip download of the master branch again. All I need to do is to place the example files in the install directory and, in case of the zip download method, adjust the include path (as described in the example files). Both methods (composer and direct download) have been tested by and worked fine for me, on the following systems:

  • Windows 7 / Apache 2.2 (Xampp) stack running PHP 5.4.7
  • Ubuntu 12.04 / Apache 2.4 stack running 5.5.3
  • Debian Squeeze / Nginx 1.4.4 stack running PHP 5.3.10-1~dotdeb.0 (with Suhosin-Patch)
  • Debian Squeeze / Nginx 1.4.4 stack running PHP 5.3.27-1~dotdeb.0 (with Suhosin-Patch)

@halbesma I can not reproduce your error. Can you please help me and follow these steps:

  1. Download the most recent zip from master branch
  2. Extract the zip to /some-path/seostats
  3. Copy
    /some-path/seostats/example/get-alexa-metrics.php to
    /some-path/seostats/get-alexa-metrics.php and
    /some-path/seostats/example/get-google-pagerank.php to
    /some-path/seostats/get-google-pagerank.php
  4. Open the example files and change the autoload include (see inline comment)
  5. Run example files

If that does not work (and you still get the error that you described upthread), please try one more thing:

  1. Save this file in /some-path/seostats/SEOstats/Common/SplClassLoader.php
  2. Open your /some-path/seostats/SEOstats/bootstrap.php and change (and save) it as follows:
/*
 *---------------------------------------------------------------
 *  Register custom PSR-0 Autoloader
 *---------------------------------------------------------------
 */

#require_once realpath(__DIR__ . '/Common/AutoLoader.php');
require_once realpath(__DIR__ . '/Common/SplClassLoader.php');

#$autoloader = new \SEOstats\Common\AutoLoader(__NAMESPACE__, dirname(__DIR__));
$autoloader = new \SEOstats\Common\SplClassLoader(__NAMESPACE__, dirname(__DIR__));

$autoloader->register();

Got it working?

@halbesma
Copy link

@eyecatchup
Most examples work as per your first instructions using the zip download method with the exception of the following errors:

Fatal error: Class 'SEOstats\Services\SEMRush' not found in /home/domainqu/public_html/SEOstats/get-semrush-graphs.php on line 28

and

Fatal error: Class 'SEOstats\Services\SEMRush' not found in /home/domainqu/public_html/SEOstats/get-semrush-metrics.php on line 28

Other than that, all is working perfectly!
Cheers!

@eyecatchup
Copy link
Owner Author

@halbesma Thanks again. Glad to hear it's working for you now.

Yep, that's why I asked for get-google-pagerank.php and get-alexa-metrics.php specifically - I tested only these 2 yesterday. Checking on the SEMRush classes later and commit a fix for it asap too.

@microthemes
Copy link

@halbesma

I'm also facing this issue:

Fatal error: Class 'SEOstats\Services\SEMRush' not found in ...

When I'm trying to use @eyecatchup SplClassLoader.php
it's return:

Warning: require(../SEOstats/Services/SEMRush.php): failed to open stream: No such file or directory in ...

So the quick solution is rename SemRush.php to SEMRush.php, I got it work! see http://domainstatistic.info?q=example.com

Rifki

@Evan-R
Copy link

Evan-R commented Feb 4, 2014

the bootstrap is not necessary if you just register a reasonable autoloader for modules in your application, something like this:

function __autoload_namespaced_module($class) {
    $path = str_replace('\\', '/', $class);

    if (file_exists($file = (PATH_TO_MODULES_DIR . $path . '.php'))) {
        require_once($file);
    }
}

spl_autoload_register();
spl_autoload_register('__autoload_namespaced_module');

@ClemensSahs
Copy link
Collaborator

He guy

It looks for me that this is a problem with the different write of "SemRush.php" and "SEMRush"

On Windows everything ok and Linux don't work this different version. The reason the windows filesystem interpret the UpCase as DownCase, too. For linux are "SemRush.php" and "SEMRush.php" tow different files...

Solution 1: rename the file name
Solution 2: rename the class name

@ClemensSahs
Copy link
Collaborator

We fix this with the commit right? so this can close?

@ClemensSahs
Copy link
Collaborator

I will close this issue now. If any one can reproduce this with the current beta (2.5.3-beta4) we can open this again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants