Calling class from vendor - codeigniter

EDIT Autoload
require __DIR__ . '/../../../vendor/autoload.php';
I am calling it this way:
$generatorSVG = new \Picqer\Barcode\BarcodeGeneratorSVG();
The error:
Message: Class 'Picqer\Barcode\BarcodeGeneratorSVG' not found
The class exist. I am not installing it via composer but copying it in the vendor folder, because I dont have composer and the server and dont have permissions to install it.
Using PHPStorm and by ctrl+click on the class leads me to it. I am wrong with the proper calling it in CI ?

Manually copying files in the vendor directory is not enough, because Composer generates a bunch of lookup files (vendor/composer/autoload_*.php) to be able to figure out where the files are located that you want to load via Composer's autoloader.
Either, you should composer require the package you want to install or require all your source files manually in your bootstrap.php or index.php.
If this is a custom package you built, be aware that Composer can also pull it directly from a Git instance, local directory, or zip file. There's no need to create a public package on Packagist or self-host it with Satis. Check the Composer docs on how to configure repositories to find out more.

Related

Installing Composer and Packagers - first time

I have never used Composer, but I want to use PHPSpreadsheet package, and it is recommended that Composer is used.
I am on a MAC using XAMPP and Netbeans.
I have installed Composer, and I have run the following command to get and install the PHPSpreadsheet package.
php ../../Composer/composer.phar require phpoffice/phpspreadsheet
I am running this in my project folder, (hence the ../../ to where Composer.phar is located.
This downloads the files into a vendor folder in my project folder.
What should I do then? Do I need to keep it in the Vendor folder, or can I move into a folder of my choice?
Netbeans has Composer options in the menus, but as far as I can see, this is for creating dependencies rather than installing packages.
I know I am totally missing the point of Composer somewhere, but have spent hours just trying to get this work.
Many thanks
You should really start with the docs -> https://getcomposer.org/doc/01-basic-usage.md
You have to keep the vendor directory - this is where all dependencies are kept. If you require more packages - then they will be installed in that directory.
After requring the package you have to load it so the PHP will know all the classes. Composer comes with great autolader. It is located by default in vendor/autoload.php. So what you have to do now is to require this file in your project. After that all classes from composer packages will be loaded automaticaly each time you use them in the code :)
I hope this will help you with this great tool. Cheers.

Refactoring Composer executable file for portability

Installing Composer locally puts a file named composer in your directory. I have taken to copying this file into each of my projects that uses Composer, so that I can run php composer in each folder.
Since this results in a lot of needlessly-repeated code, I want to refactor the composer file into something minimal. The most logical solution would be to refer to a class that resides in the vendor folder. Why is there so much code in the composer file anyway? I'd prefer not to put the code into my own libraries if I don't have to.
For comparison, here's a sample of code in a typical phpunit file:
require_once 'my_bootstrap.php';
spl_autoload_register(array(MyBootstrap::class,'autoload'));
// Do some other stuff here.
\PHPUnit_TextUI_Command::main();
I want to reduce the composer file in each project to something like this.
I do not want to install Composer globally. I want each project to be an environment unto itself and not have to instruct another developer to install anything other than PHP.

How to use composer included items?

In my composer.json file I added "kigkonsult/icalcreator": "v2.23.7" to the required list, and after doing the install I see that directory in existence under the vendor dir. I can't figure out how to "use" it though in my PHP files in this project.
If I weren't using composer I'd just require the icalcreator.php file in that area. What's the right syntax when I include it via composer?
Once the package is installed you need to add the composer autoloader somewhere in your project.
require_once('/path/to/vendor/autoload.php');
Normally Composer uses PSR-4 autoloading, but it looks like this package doesn't. It just adds it's own autoloader to composer so just including the Composer loader should be sufficient.
Once the autoloader is added you can just use the package however you need. Composer will automatically register the packages custom autoloader and PHP will use that to load the appropriate file.

A composer's package doesn't exist anymore, and I have a copy. What can I do?

Time ago, I installed a dependency on a Symfony project. It was the package mograine/calendar-bundle, but now this project doesn't exist anymore and has disappeared from github. It was a fork of another package with some modifications that I need for the project I'm working on.
Of course, I have a copy of the package (under vendor/mograine folder). But, currently I'm unable to run the composer install order, because this package doesn't exist.
And my question is: What can I do to solve this problem? Can I tell composer that this package is installed locally? If so, what should I do to install the package locally? Or I must create a github account and upload all the original files?
If its a normal symfony project, you can simply move it to your src folder resp. "copy past the namespace directories to the src directory" and remove it from the composer.json.
The src folder is autoloaded f.e.
"autoload": {
"psr-0": {
"": "src/"
}
Take care that the namespaces and pathes are correct.
Also see here: moving bundle from vendor to src directory

forcing composer to regenerate autoloads when composer.json of a dependency is changed?

My workflow for developing Symfony bundles is the following:
install Symfony
create a git repo for the new bundle, put a composer.json file in there
require the new package in the top-level composer.json, using #dev version
composer update newpackage => the package is downloaded, using git clone
work on the git clone inside vendors, committing and pushing from it
This is all fine and dandy, but seems to break in one specific case:
if I alter the 'autoload' tag of the already-installed package, it seems that Composer has a hard time taking it into account:
I tried 'composer dumpautoload', and it does nothing
I do not want to remove the composer.lock file, as I do not want other packages to be updated to a newer version, I only want to alter the autoload config of that one package
I tried removing by hand vendor/composer/installed.json, and what happened is that Composer re-downloaded all the vendors and wiped any data which happened to be in there at that moment
The same problem manifested itself when I did alter the autoload section of the package on a separate clone, pushed the changes to git and ran 'composer update mypackage' - although that might have been related to packagist not having received the ping from github.
I can of course alter by hand the composer.lock and vendor/composer/installed.json files, but that seems too hackish. It also does not gives the guarantee that user downloading the package the 1st time will see it working.
Try:
./composer.phar dumpautoload -o
It reads the composer.json files and rewrites all the autoload files which pick up the new paths.
dumpautoload uses the package information from vendor/composer/installed.json and not the individual composer.json files. You need to change the autoload info there, too.
The file installed.json is only being update when you run a
composer update

Resources