I've created a small command line tool in PHP and I've also packed that as a PHAR archive.
Next thing I did was publish my archive to packagist.org aka composer.
I can now install my PHAR package through composer like so:
composer global require acme/mypackage
This installs my package fine. And I'm able to run it through command as well.
So far so good, but here comes the problem I´m currently facing.
I have another project should use acme/mypackage. I want that project to reference a class that is packed into that PHAR. Something like this:
<?php
class SomeClass extends AcmeClass {
}
The problem is that the PHP code doesn't recognize the AcmeClass class. Makes sense, because it´s obviously "globally" installed somewhere on the system.
How do other libraries solve this issue? If I'm not mistaken then PHPUnit does something similar right?
How can I solve this issue?
You'll need to add a composer.json file to the root of your project:
The first (and often only) thing you specify in composer.json is the require key. You're simply telling Composer which packages your project depends on.
{
"require": {
"monolog/monolog": "1.0.*"
}
}
Next, you'll need to autoload your dependencies.
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can simply include this file and you will get autoloading for free.
require 'vendor/autoload.php';
https://getcomposer.org/doc/01-basic-usage.md
Related
I want to install html-to-markdown without composer but I can't figure it out, with other libraries like Parsedown it worked just by using file location, but with html-to-markdown I don't know what I should use
This is the link for html-to-markdown
In your link there is a chapter How to use it with step by step instructions for installation.
Make sure to add require 'vendor/autoload.php'; to the top of your script, where you want to use the package.
First, use GitHub's "Code/ Download ZIP" option to get the code:
To figure out the requirements of an application meant to be installed with Composer, you need to inspect the composer.json file in the root directory:
The require key tells us the PHP extensions and third-party libraries needed:
"require": {
"php": "^7.2.5 || ^8.0",
"ext-dom": "*",
"ext-xml": "*"
},
In this case, the library is standalone, so you just need to verify your PHP installation meets the requirements. If it happened to need other packages, you'd have to repeat this for every package involved.
The autoload key tells us how the library locates and opens the files with class definitions:
"autoload": {
"psr-4": {
"League\\HTMLToMarkdown\\": "src/"
}
},
This library depends on Composer's PSR-4 autoloader implementation. We'll get back to this.
There may be other important information in the file, but in this particular case this is pretty much everything.
Regarding the autoloader... If your codebase already implements PSR-4, you just need to configure a new League\HTMLToMarkdown namespace and download the source code in the location expected by your autoloader, keeping the folder structure intact. Otherwise, I can think of a number of options, none of them particularly attractive:
Write your own autoloader.
Find a third-party PSR-4 autoloader.
Get a list of all relevant source PHP files and load them all manually:
$ find . -name '*.php'
./src/Environment.php
./src/Configuration.php
./src/Element.php
./src/ElementInterface.php
./src/PreConverterInterface.php
./src/HtmlConverterInterface.php
./src/ConfigurationAwareInterface.php
./src/Converter/TextConverter.php
./src/Converter/TableConverter.php
./src/Converter/ListItemConverter.php
./src/Converter/ListBlockConverter.php
./src/Converter/PreformattedConverter.php
./src/Converter/ConverterInterface.php
./src/Converter/DivConverter.php
./src/Converter/BlockquoteConverter.php
./src/Converter/HorizontalRuleConverter.php
./src/Converter/DefaultConverter.php
./src/Converter/CodeConverter.php
./src/Converter/ImageConverter.php
./src/Converter/HardBreakConverter.php
./src/Converter/ParagraphConverter.php
./src/Converter/EmphasisConverter.php
./src/Converter/CommentConverter.php
./src/Converter/HeaderConverter.php
./src/Converter/LinkConverter.php
./src/HtmlConverter.php
Make this into a list of require '...'; commands and play around with file order until you don't get errors.
I'm trying to make a composer package, but I'm struggling to setup the autoload to work in the project where I'm developing it.
I have two projects:
Project Foo (where I'm developing the package).
Project Bar (where I've installed the package: composer require myhandle/mypackage).
In Project Foo I (obviously) have to be able to use the package as well. But setting up the autoload in my own package is not working.
However... When I commit the changes to Github, update the package at Packagist and run composer update in Project Bar, then it works(?!).
And wierdly enough... If I do composer update from Project Foo (the project where it isn't working). So updating the package to it's current version (?), makes it start working.
So it must somehow be related to how I've setup the autoload.
A note:
I started the package by making a new (empty) folder in the vendor directory, and then build the package there. I figured it was smart, since it then would mirror how it would look had I composer required the package.
I don't know if this is bad practice, since Composer no longer 'are in control' of all files in the vendor directory.
There are tons of guides on how to make a composer package out there, - but non of them explains about a good way to structure the files in the project where the package is being developed.
Here's what I do to 'get the error' (in Project Foo):
Create new class file, such as: myhandle/mypackage/src/Test.php
Then I instantiate it like this: $test = new MyNamespace\MyPackageName\Test();
And then I get the error:
Fatal error: Uncaught Error: Class 'MyNamespace\MyPackageName\Test' not found
And this is what works in Project Bar (the very same code).
I can't find a guide on how to correctly setup autoload in the package I'm developing. I'm using this autoload file, that I found in another composer project. I've put it in the root of my project. It looks like this:
<?php
namespace MyNamespace\MyPackageName;
spl_autoload_register(function($cls) {
$cls = ltrim($cls, '\\');
if (strpos($cls, __NAMESPACE__) !== 0) {
return;
}
$classWithoutBaseNamespace = str_replace(__NAMESPACE__, '', $cls);
// Load files from 'src' directory based on their class name without
// the StoutLogic\AcfBuilder namespace.
$path = dirname(__FILE__).
DIRECTORY_SEPARATOR.
'src'.
str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutBaseNamespace).
'.php';
require_once($path);
});
I can't find it in the Composer Documentation, how to set it up in a new project/package. However I can find a bazillions guides on how to use autoload.
As yivi and Daniel Protopopov pointed out:
Check the documentation at getcomposer.org regarding autoloading
Delete your custom autoloader definition, register your namespace in composer.json (hoping you follow PSR-4 already), run composer dump-autoload.
Last but not least, when- and wherever you need to use it, just include the
require __DIR__ . '/vendor/autoload.php';
Good afternoon, I will start off by saying that I've never included a composer project in my projects unless it was a composer package for the Laravel framework. Where you "require" it and add it to the providers array and aliases if needed.
Now, the problem. I have a composer package I'm trying to play around with https://github.com/WHAnonymous/Chat-API the problem is i dont know how to include it into myu project since its not really made "for" laravel. So I'm trying to use plain php without a framework but I have no idea how to "load" the package in, tried googling and only found information regarding building a package.
To clarify: I can install the package fine, its the php part of "loading" the package in my index.php file that im struggling with, pretend the index.php file is empty.
Can someone please help me?
After installing the package with composer, the composer has generated an autoloader that you can include with:
require_once 'vendor/autoload.php';
Then you can use the classes of the package without further includes. In your example this might be:
// Create an instance of WhatsProt.
$w = new WhatsProt($username, $nickname, $debug);
(taken from https://github.com/WHAnonymous/Chat-API/blob/master/examples/exampleRegister.php)
Note that this line from the example is not necessary, when you use the composer autoloader:
require_once('../src/whatsprot.class.php');
Assuming you have composer installed and have gone through the basics in the link posted by Paul.
You would run the following
~/composer install (same directory your composer.json file resides in).
Contents of composer.json file would be:
{
"require" : {
"whatsapp/chat-api" : "2.5.4"
}
}
I am trying different libraries via composer and on all of them I hit a similar error:
Fatal error: Class 'MyRestApi\Admin' not found in /usr/home/www/tmp/index.php on line 6
I have tried short and long path names:
set_include_path('/usr/home/www/vendor/marcj/php-rest-service/RestService');
require_once('/usr/home/www/vendor/marcj/php-rest-service/RestService/Server.php');
I have tried require, include, require_once, set_include_path.
Also I have tried
require "../vendor/autoload.php";
and
require "/home/www/vendor/autoload.php";
Always the same error trying to find the first class.
This particular library https://github.com/marcj/php-rest-service it the one I am trying.
How do you get a composer library to work without an autoload? (I am assuming this has something to do with my issue)
You don't. Using the autoloader of Composer is one of the central contracts that every package that gets installed with Composer relies on.
In theory you could try to invent your own autoloading. In practice: Why?
Your code is missing a require "vendor/autoload.php"; - the path to vendor depends on where your file is that wants to include the autoloader.
Note that if you add your own package and install it via Composer, it must not include the autoloader by itself - the code that uses this package is supposed to do it.
I'm hoping someone can spot what I've forgotten to do. Here are my steps:
Downloaded and unpacked the ZendFramework-2.3.5 into /usr/share.
Updated include_path in my php.ini file to include '/usr/share/ZendFramework-2.3.5/library' per the INSTALL.md, and restarted Apache to confirm the path is set (now ".:/usr/share/php:/usr/share/ZendFramework-2.3.5/library").
Created a test script in my web document root (using the class 'CamelCaseToUnderscore' as an example):
use Zend\Filter\Word\CamelCaseToUnderscore;
$filter = new CamelCaseToUnderscore();
echo $filter->filter('BigsAndLittles');
...and I get the fatal error "class 'zend\filter\word\camelcasetoseparator' not found".
In order to do use Zend classes like this, do I need to do some additional configuration or create an autoloader or something to find them? Seems like this should have worked. If I include the CamelCaseToUnderscore.php file in a require_once statement, then I get a fatal error that it's parent class doesn't exist (CamelCaseToSeparator.php). What am I missing?
You can use require 'Zend/Mvc/Application.php' to test if your include path is correct, but you will need an autoloader:
http://framework.zend.com/manual/current/en/modules/zend.loader.standard-autoloader.html.
You can find an example here (lines 18-20):
https://github.com/zendframework/zf2/blob/master/demos/Zend/Feeds/consume-feed.php
I strongly suggest using composer as it will save you a lot of time troubleshooting your include paths, but it also allows you manage version better. It makes it easier for other developers and to deploy your code.
Starting with composer is very easy, just install it and create composer.json:
https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup
Run:
composer require zendframework/zendframework
Composer will download all libraries to vendor folder and will generate an autoloader, all you have to do is to include
require 'vendor/autoload.php';
https://getcomposer.org/doc/01-basic-usage.md#autoloading
Most popular PHP frameworks use composer for managing dependencies:
https://github.com/zendframework/zf2/blob/master/composer.json
https://github.com/symfony/symfony/blob/2.7/composer.json