Composer in autoload_static.php use class that I don't need them in every app request.
'd5fa61a7f6cbc1df09dd4df84549a2dc' => __DIR__ . '/..' . '/rospdf/pdf-php/src/Cpdf.php',
'2d15964294879de66053d54f6bde65d7' => __DIR__ . '/..' . '/rospdf/pdf-php/src/Cezpdf.php',
How to remove them from this autoload file? I can delete/comment them manually but every Composer update this file is re-generated.
I try to add in my main composer.json:
"exclude-from-classmap": ["vendor/rospdf/pdf-php/src/"]
& run composer dump-autoload bo those class are still in there.
You can trick the autoloader of composer and let him think those are already loaded:
<?php
// Setting global variable:
$GLOBALS["__composer_autoload_files"] = [
"d5fa61a7f6cbc1df09dd4df84549a2dc" => true,
"2d15964294879de66053d54f6bde65d7" => true,
];
require "vendor/autoload.php";
But this needs to happen before the vendor/autoload.php is included.
Related
I'm having trouble understanding with doesn't Composer autoloads the packages I required.
My current composer.json file has the following:
{
"require": {
"atlas/orm": "#dev"
},
"require-dev": {
"atlas/cli": "#dev"
}
}
It was supposed to generate a Namespace in the /vendor/composer/autoload_namespaces.php file. But it doesn't. The file only has the following:
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Psr\\Log\\' => array($vendorDir . '/psr/log'),
);
Of course, when I try to use the "require DIR . '/vendor/autoload.php';" to autoload the package and then use its classes, it does not work.
Any idea on how can I solve this?
For requiring in all of the installed dependencies, you have to require 'autoload.php'. For autoloading(PSR-4), in the composer.json file, you have to give a name under which everything will be namespaced and the folder name from which files will be autoloaded.
"Namespace_name\\":"folder_name"
Note: The backslash after the namespace_name needs to be escaped, hence the extra backslash.
Then run composer dump-autoload -o
Trying to get Stripe library up and running. Get failed opening required 'vendor/autoload.php' error.
autoload.php is there in /vendor
ran composer update, then composer install , didn't help.
config.php
<?php
require_once('vendor/autoload.php');
$stripe = array(
"secret_key" => "XXXXXXXXXXXXXXXXXXXXXX",
"publishable_key" => "XXXXXXXXXXXXXXXXXXXXXX"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>
autoload.php
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit18eff69591fc6597a849a05ec4932261::getLoader();
It looks like a problem with your relative path. Because this "script is in /app/modules/stripe" and the vendor folder "is /vendor", then you can use the constant __DIR__ to reference the included script's location, and then up 3 relative directories.
require_once(__DIR__.'/../../../vendor/autoload.php');
Try to change the require_once to this:
require_once __DIR__ . '/vendor/autoload.php';
try this.Work for me.
composer update --no-scripts
I install the PhpExcel library in my project with composer :
"require": {
"phpoffice/phpexcel": "dev-master"
}
I regarded in my composer folder : composer/autoload_namespaces.php :
return array(
'PHPExcel' => array($vendorDir . '/phpoffice/phpexcel/Classes'),
.....
)
Now in my controller I do :
$objPHPExcel = \PHPExcel_IOFactory::load($inputFile);
But I have the error :
"Class 'PHPExcel_IOFactory' not found". I don't understand where is my mistake..Can you help me please ? Thx in advance
When you create composer.json file you should run
composer install
And next in your code you should include generated autoloader:
require __DIR__ . '/vendor/autoload.php';
And it's all. Should works. You don't modify nothing in vendor directory.
I have to environments i'm working developing my API (PHP based):
Local development: Mac OS Yosemite - running PHP 5.5.20
Production server: Ubuntu server - running PHP 5.5.9
My code uses composer for auto loading as followed:
{
"require": {
"facebook/php-sdk": "#stable",
"everyman/neo4jphp": "dev-master",
"predis/predis": "1.0.1",
"aws/aws-sdk-php": "2.*"
},
"autoload": {
"psr-0": {
"PicoCore\\": "",
"PicoCore\\Authentication\\" : "PicoCore/authentication",
"PicoCore\\Aws\\" : "PicoCore/aws",
"PicoCore\\Cache\\" : "PicoCore/cache",
"PicoCore\\Database\\" : "PicoCore/database",
"PicoCore\\Database\\Managers\\" : "PicoCore/database/managers",
"PicoCore\\Facebook\\" : "PicoCore/facebook",
"PicoCore\\Objects\\" : "PicoCore/objects",
"PicoCore\\Rest\\" : "PicoCore/rest",
"PicoCore\\Configuration\\" : "PicoCore/configuration",
"PicoCore\\Configuration\\Api\\" : "PicoCore/configuration/api",
"PicoCore\\Configuration\\PictureReceiver\\" : "PicoCore/configuration/pictureReceiver",
"PicoCore\\Configuration\\PictureUploader\\" : "PicoCore/configuration/pictureUploader",
"PicoCore\\Scripts\\" : "PicoCore/scripts",
"PicoCore\\Times" : "PicoCore/times"
}
}
}
This is my loading function:
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'PicoCore\\Times' => array($baseDir . '/PicoCore/times'),
'PicoCore\\Scripts\\' => array($baseDir . '/PicoCore/scripts'),
'PicoCore\\Rest\\' => array($baseDir . '/PicoCore/rest'),
'PicoCore\\Objects\\' => array($baseDir . '/PicoCore/objects'),
'PicoCore\\Facebook\\' => array($baseDir . '/PicoCore/facebook'),
'PicoCore\\Database\\Managers\\' => array($baseDir . '/PicoCore/database/managers'),
'PicoCore\\Database\\' => array($baseDir . '/PicoCore/database'),
'PicoCore\\Configuration\\PictureUploader\\' => array($baseDir . '/PicoCore/configuration/pictureUploader'),
'PicoCore\\Configuration\\PictureReceiver\\' => array($baseDir . '/PicoCore/configuration/pictureReceiver'),
'PicoCore\\Configuration\\Api\\' => array($baseDir . '/PicoCore/configuration/api'),
'PicoCore\\Configuration\\' => array($baseDir . '/PicoCore/configuration'),
'PicoCore\\Cache\\' => array($baseDir . '/PicoCore/cache'),
'PicoCore\\Aws\\' => array($baseDir . '/PicoCore/aws'),
'PicoCore\\Authentication\\' => array($baseDir . '/PicoCore/authentication'),
'PicoCore\\' => array($baseDir . '/'),
'Guzzle\\Tests' => array($vendorDir . '/guzzle/guzzle/tests'),
'Guzzle' => array($vendorDir . '/guzzle/guzzle/src'),
'Everyman\\Neo4j' => array($vendorDir . '/everyman/neo4jphp/lib'),
'Aws' => array($vendorDir . '/aws/aws-sdk-php/src'),
);
When i'm running my code on my local development - it works perfectly, so I pushed it to my remote repository and pull it from the production server.
When i'm trying to run my code in my Production server I receive a Class not found error indicating that my autoloading didn't run properly.
Any ideas what could be the reason ?
UPDATE:
When i'm trying to load the external libs (like AWS) it does work, so the problem is defiantly something with how my folders are being loaded.
UPDATE:
This is the class i'm trying to load from index.php:
<?php
//require Pico Core autoload
require 'core/vendor/autoload.php';
//require Pico Api autoload
require 'vendor/autoload.php';
use PicoCore\Rest\ApiInitializer;
use PicoCore\Configuration\Error;
use PicoApi\Managers\ApiManager;
try {
//initialize the API
ApiInitializer::initialize();
//initialize a new Api with the request
$api = new ApiManager($_REQUEST['request']);
echo utf8_encode($api->processApi());
} catch (Exception $e) {
echo json_encode(Array(Error::jsonErrorField() => $e->getMessage()));
}
This is the error I receive Class 'PicoCore\Rest\ApiInitializer' not found
The structure is:
api
|
core
|
PicoCore
|
folders..
rest
|
ApiInitializer.php
folders...
vender
PicoApi (folder)
vendor (foldeR)
index.php
It's your wrong autoloading definition.
You want to use the class
PicoCore\Rest\ApiInitializer
And you have these files:
api/index.php
api/vendor/... (with your required external dependencies)
api/composer.json (with the autoloading mentioned in your question)
api/core/PicoCore/rest/ApiInitializer.php
And you have this autoloading configured:
"autoload": {
"psr-0": {
"PicoCore\Rest\" : "PicoCore/rest"
}
}
This is a mismatch. When you use the class PicoCore\Rest\ApiInitializer, composer will try to locate an entry with a prefix of that class. It will sucessfully detect that classes with prefix PicoCode\Rest can be found via PSR-0 rules in the directory (relative to the position of composer.json) PicoCore/rest.
First strange thing: There is an additional directory named "core" here, and you seem to include TWO autoloaders. Don't do that, Composer works best with only one autoloader.
Second thing: If I ignore that "core" directory for a bit, the PSR-0 rules state that the classname will be converted to a path - and then be searched in the path given for the prefix.
PicoCore\Rest\ApiInitializer as a path is PicoCore/Rest/ApiInitializer.php (note the uppercase "R" in "Rest"), and the path this is to be found is PicoCore/rest/PicoCore/Rest/ApiInitializer.php.
You don't have this file. And thats why Composer cannot find and load it. And this is even without counting this "core" directory level, it will also not be found if you used two composer.json files, one in api and one in api/core, and the one in api/core was used to find the class.
Suggestions:
Use PSR-4 for every class that is inside a namespace.
Don't lowercase namespace or classname parts for the filesystem.
Shorten your autoloading definition. If you'd rename all those lowercase directories below PicoCore into the proper uppercase variants that are being used in your namespace, you will only need one line of autoloading definition, defining the PicoCore prefix. The rest is done by PSR-4 (or PSR-0).
MacOS is using a case insensitive file system by default, and it is considered a bad thing to switch it to case sensitive: https://apple.stackexchange.com/questions/71357/how-to-check-if-my-hd-is-case-sensitive-or-not That's why it is working on Mac, but not Linux.
I'm trying to use the Stripe php api in a Bolt extension but it's having trouble finding the class. I added the Stripe library to composer.json:
"require": {
"stripe/stripe-php": "1.*"
}
And ran composer install. myextension/vendor/composer/autoload_classmap.php now shows the classes loaded:
$vendorDir = dirname(dirname(__FILE__));
return array(
'Stripe' => $vendorDir . '/stripe/stripe-php/lib/Stripe/Stripe.php',
'Stripe_Account' => $vendorDir . '/stripe/stripe-php/lib/Stripe/Account.php',
'Stripe_ApiConnectionError' => $vendorDir . '/stripe/stripe-php/lib/Stripe/ApiConnectionError.php',
'Stripe_ApiError' => $vendorDir . '/stripe/stripe-php/lib/Stripe/ApiError.php',
...
And now I'm trying to use it in the extension like this:
use Stripe, Stripe_Customer, Stripe_Charge, Stripe_Plan, Stripe_Coupon, Stripe_Error;
public function initialize()
{
$stripe = new Stripe();
$stripe->setApiKey($this->config['stripe_key']);
But I get the error:
Error: Class 'Stripe' not found
File: extensions/local/andyjessop/myextension/Extension.php
I still haven't got my head round autoloading, so I think I'm doing something basic wrong, but I can't see what it is. Can anyone help?
You should include the composer autoload file.
require_once('vendor/autoload.php');
Why don't you try this command
composer require stripe/stripe-php
also make sure you are updating your composer file, this will work cheers