Class not found with composer autoload in different servers - php

I have a very simple PHP/Composer application with the following structure:
- src
- content
- test
- Sandbox.php
Sandbox.php has only a static function to print "test" and its namespace is
namespace MyApplication\Content\Test;
My autoload.php has MyApplication an "autoload" property.
"autoload" : {
"psr-4": {"MyApplication\\": "src/"}
},
I run composer install --no-dev in an Windows environment with XAMPP and in a test.php file I do (for the sake of a very simple test):
$autoloadFile = __DIR__ . '/wp-content/plugins/sandbox/vendor/autoload.php';
require $autoloadFile;
echo 'autoload = ' . file_exists($autoloadFile);
echo '<br />';
echo 'class_exists = ' . class_exists('MyApplication\Content\Test\Sandbox');
When I run this test.php file locally, it works perfectly. MyApplication is loading Sandbox class.
However, when I release this to my server which is a Linux based server but running on the same PHP version, Sandbox class is not found.
I made sure my /vendor/ folder is properly uploaded as well.
I'm wondering if the problem is happening because I'm running composer install on a Windows environment while it should be running the same command in my server (which I can't at the moment). Shouldn't the /vendor/ folder upload be enough to make the autoload classes work well?

Path to your file is src/content/test/Sandbox.php and according to PSR-4 it should be src/Content/Test/Sandbox.php - on Windows it does not matter, but on Linux it does.

Related

Class not found exception in PHP from composer

I am trying to include a package from composer, but I am receiving a class not found error.
I have tried the below possibilities.
$supermeteor = new \Supermeteor\Supermeteor('XXXXXXXX');
and
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor('xxxxxxxx');
Packages composer.json:
"psr-4": {
"Supermeteor\\": ""
}
Packages namespace :
namespace Supermeteor;
Packages class name :
class Supermeteor() {}
Error message
Uncaught Error: Class 'Supermeteor\Supermeteor' not found in
C:\path\to\my\file.php:16
I just tested your package locally, and it seems to work fine for me using the same code as you provided in your question. This is how I tested it.
1. Create a new project
Create a new directory on your computer.
2. Add the package to a new project using Composer
Locate your new directory on the command line and add the package to your projects autoloader by running the below composer command.
composer require supermeteor/sdk-php
3. Use the package
Create an index.php file in the same directory as your composer.json and add the below code.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor("xxx");
4. Test the results
In the terminal window start a new php server to serve your project.
php -S localhost:8089
Now access the site via your browser at http://localhost:8089.

having difficulty setting up phpepub library

i have downloaded phpepub via composer
then started to run the test file to understand how to use the library but it throws an error
Class 'com\grandt\EPub' not found
and then i started to view the test folder and opened the file exampletest1.php which also threw an error saying that
Class 'PHPePub\Core\Logger' not found
i'm thinking a way of working out this error for a while now checked the privileges (which is fine) also the file is also present in the folder
here is the file structure of the library
phpepub/
legacy/
src/
PHPePub/
Core/
structure/
Logger.php
.
.
.
Helpers/
tests/
demo/
EPub.Example1.php
.
.
.
composer.json
vendor/
composer/
grandt/
phpzip/
.
.
.
README.md
test.php
ReadMe.html
.
.
.
.
composer.json
You need to require the vendor/autoload.php file in each file where you're using components installed using composer.
test.php :
<?php
require_once __DIR__.'/vendor/autoload.php';
//...
test/exampletest1.php
<?php
require_once __DIR__.'/../vendor/autoload.php');
//...
See Basic Usage - Autoloading in Composer Documentation.
Usage in your projects
In the root directory of your project, add "grandt/phpepub": ">=4.0.3" to your composer dependencies and run composer install.
Let's say your project directory structure is :
project
vendor
public
index.php
composer.json
When you run composer install, Composer creates a directory vendor/ in the project root and generates an autoload file vendor/autoload.php.
To use the installed libraries in index.php, require the autoload file :
index.php :
<?php
require_once __DIR__."/../vendor/autoload.php";
//...
For a quick detailed explanation, try reading Juan Treminio - Composer Namespaces in 5 minutes

Composer autoload won't work after deployment

I am relatively new to using composer and autoload to organize my code. I have a git repository and on my local machine, I set up composer in the root dir of my project. I specified everything in a composer.json needed to get running. Using "composer install", all libraries are automatically installed.
{
"name": "my/repo",
"description": "bla",
"version": "1.2.3",
"require":
{
"php": "5.6.*",
"geraintluff/jsv4": "1.*",
"lcobucci/jwt": "^3.0"
},
"autoload":
{
"psr-4":
{
"MyNamespace\\": "src/"
}
}
}
So - once I ran "composer install" on my local machine, everything was autoloaded in my code. Fine.
But now I need to deploy the whole thing on another linux system. So i pull from the git and run composer install. All the libraries are fetched and the autoload file shows up in vendor/
Yet, I cannot use autoload (yes, I did require_once(__DIR__ . '/../vendor/autoload.php');). Everytime I try to instantiaze a class, i get a
PHP Fatal error: Class 'X' not found in /var/www/bla/x.class.php on line 123
Using use X; does not solve the problem, nor does trying to instantiate the class with its full Namespace name (e.g. $x = new \A\B\X();)
Here is the folder structure (if this matters):
+ src/
| + X.class.php // namespace here is "MyNamespace"
| + Y.class.php // same namespace
+ test/
+ run.php // namespace is "Test"
Here is a snippet of this code (run.php):
<?php namespace Test; // different namespace than the rest of the code
// making the namespace also "\MyNamespace" wouldnt work either
require_once(__DIR__ . '/../vendor/autoload.php');
use \MyNamespace\Y; // whether this line is here or not does not change the error
session_start();
// same error as with "just" implements Y {}
class SomeClass implements \MyNamespace\Y {
// ...
}
?>
So here, the Fatal error is thrown for the line where Y is extended. No matter if I use the full namespace or not. Only thing that will help is a require_once()...
So, this forces me to go back to the cumbersome way of doing all the require/includes myself!? Is there any way to solve this?
PS: composer dumpautoload wont help
PPS: composer validate shows no errors
For PSR-4 compliance, your file structure should be:
+ src/
| + X.php
| + Y.php
Note the removal of the .class.php suffix. http://www.php-fig.org/psr/psr-4/

Fatal error: require(): Failed opening required 'C:\wamp\www\sep24\e/src/functions.php' (include_path='.;C:\php\pear')

Tried to run the trans.php program from wamp server from the path
C:\wamp\www\sep24\e\trans.php
I have included the AWS folder in
C:\wamp\www\sep24\e\Amazon\
And AWS credential file in wamp/www folder as well user directory for the access
C:\wamp\www\.aws\credentials & C:\Users\username\.aws\credentials
This is my program
<?php
define('ROOT', dirname(__FILE__));
require ROOT . '/vendor/autoload.php';
use Amazon\Aws\ElasticTranscoder\ElasticTranscoderClient;
-------------
------------
// no error here.
?>
When i'm trying to run the program, I get this error
Fatal error: require(): Failed opening required 'C:\wamp\www\sep24\e/src/functions.php' (include_path='.;C:\php\pear') in C:\wamp\www\sep24\e\vendor\composer\autoload_real.php on line 54
I have included all the packages of AWS which I downloaded from the git.
What change should I make ?
There are two main problems are:
1 Composer Autoloading
The AWS dependency needs to be downloaded with Composer,
if you want the Composer Autoloader to work correctly.
Do not move folders around, when working with Composer.
The autoloading expects the files and folders inside the vendor folder.
I have included all the packages of AWS which I downloaded from the git.
You don't need to do this manually.
2 The use statement is wrong.
Change use Amazon\Aws\ElasticTranscoder\ElasticTranscoderClient;
to use \Aws\ElasticTranscoder\ElasticTranscoderClient;
3 Example Application
Because it is your third question and you seem to have problems with the application structure in connection with Composer, i will provide a simple PHP application template to demonstrate how you work with the AWS dependency.
This example provides a basic namespaced PHP application and includes the Client class from the AWS dependency(, which you have to fetched by Composer).
You find the file over here:
https://www.dropbox.com/s/q1b406thgu3146n/php-app-composer-aws.zip?dl=0
Extract the test folder into your www folder.
Then execute a composer install and run index.php.
You will end up with a error from TranscoderClient, because it expects a configuration. Not part of the problem.
Use composer.
Create testaws directory and put composer.json file with content below (you can adjust it to your needs for example PHP version or dev packages)
{
"name": "yourname/sampleapp",
"description": "Sample app",
"require": {
"php": ">=5.5.0",
"aws/aws-sdk-php" : "dev-master"
},
}
run composer install
then in index.php in testaws directory put this line in index.php
require __DIR__ . '/vendor/autoload.php';
After you do this steps it should work. More about composer you will find there
Also you can find sample project here
Delete the vendors folder and run composer install.

Installing and working with PHPUnit

I am having a hard time trying making PHPUnit work. My Directory is for php is C:/wamp/bin/php/php5.4.3 Now, I read PHPUnit does not work without PEAR, so I got the go-pear.phar file and placed it in C:/wamp/bin/php/php5.4.3/PEAR/ ran the go-pear.phar file and installed in on the system. third, I installed PHPUnit using Git Bash and put it in C:/wamp/www/local/unit-testing/ (Not sure if the directories are correct)
Now, I created a simple UserTest file
<?php
require_once "phpunit/PHPUnit/Autoload.php";
require_once "User.php";
class UserTest extends PHPUnit_Framework_TestCase
{
protected $user;
// test the talk method
protected function setUp() {
$this->user = new User();
$this->user->setName("Tom");
}
protected function tearDown() {
unset($this->user);
}
public function testTalk() {
$expected = "Hello world!";
$actual = $this->user->talk();
$this->assertEquals($expected, $actual);
}
}
And tried to require this file and run it from the command line in Windows. But, due to lack of instructions on where this files should exactly be, I can't seem to run it.
As of now the problem is command line does not recognize the PEAR command. Even though, I had run PEAR_ENV.reg file to add PATH variables to the file.
secondly, I am not sure where exactly PEAR & PHPUnit should be installed in my current structure. I keep all my php pages/project in C:/wamp/www/local/<-- I need to test files in this directory.
The PHPUnit batch file should be in your path. Then running PHPUnit from the command line would be done in a shell, with the current directory being where you have installed everything, and it assumes that your User.php file is there as well.
I sue a bootstrap.php file to run from my source code directory.
<?php
/**
* This file is used to configure the basic environment for testing in PHPUnit.
*/
// PHP and Web server settings
error_reporting(E_ALL | E_STRICT);
date_default_timezone_set("America/Toronto"); // Set the default timezone
$_SERVER['SERVER_NAME'] = 'http://xxx'; // Set Web Server name
// Process the Include Path to allow the additional applications to be set.
$IncludePaths = explode( PATH_SEPARATOR, get_include_path() );
$NewIncludePaths = array_merge( $IncludePaths, array(dirname(__FILE__) ));
set_include_path( implode( PATH_SEPARATOR, array_unique($NewIncludePaths))); // Update Include Path
//define('PHPUNIT_RUNNING', 1); // Indicate to the code that Automated Testing is running.
?>
I was able to follow the PEAR Installation Instructions for PHPUnit and get things up and running once the directory where PHPUnit.bat existed was in my DOS Path.

Categories