Using Composer autoload inside a ZIP package - composer-php

I've been trying to get composer to generate classmaps for an API provided by a 3rd party company (OtherCompany in the example configuration) but have been unable to get it to work the way I think it should. However, I'm very new to composer so I may be completely wrong.
The situation is as follows. I have a project directory which contains the following composer.json (which does not generate classmaps for api.php):
{
"name": "company/company-library",
"description": "A useful library",
"type": "library",
"license": "GPL-3.0-or-later",
"authors": [
{
"name": "company",
"email": "hello#company.com"
}
],
"require": {
"othercompany/otherlibrary": "^1.0"
},
"config": {
"optimize-autoloader": true
},
"autoload": {
"psr-4": {
"Company\\CompanyLib\\": "src/"
}
},
"minimum-stability": "dev",
"repositories": [
{
"type": "package",
"package": {
"name": "othercompany/otherlibrary",
"type": "library",
"description": "Eases the integration of othercompany APIs into company's useful library",
"homepage": "https://otherlibraryportal.othercompany.com",
"version": "1.0",
"dist": {
"url": "../../../../repository/othercompany-otherlibrary-1.2.zip",
"type": "zip"
},
"require": {
"php": ">=5.6.1",
"phpseclib/phpseclib": "3.0.18"
},
"config": {
"optimize-autoloader": true
},
"autoload": {
"classmap": ["api.php"]
},
"minimum-stability": "dev"
}
}
]
}
The package "othercompany/otherlibrary" is contained in a ZIP file which only contains 2 files in a folder named othercompany-otherlibrary-1.2:
api.php
readme.txt
After a 'compose install', these files are stored in the directory:
vendor/othercompany/otherlibrary
The api.php file contains a number of classes which I would like to autoload. However, using the configuration above and various modifications of the classmap directive, have not been able to achieve the desired effect. Only if I move the classmap directive to the root level autoload directive in composer.json and update the path to vendor/othercompany/otherlibrary/api.php, classmaps are generated for the package my libary depends on. Snippet:
"autoload": {
"psr-4": {
"Company\\CompanyLib\\": "src/"
},
"classmap": ["vendor/othercompany/otherlibrary/api.php"]
}
Although this works and classmaps are now generated, I cannot help but think that this should not work this way as each package should independently be able to specify autoload options. If this is correct, what would be the right way to specify autoload options for the othercompany/otherlibrary package?
ADDITIONAL INFORMATION
api.php:
<?php
// Creates the API Request from the context
class APIRequest {
}
// API Response
class APIResponse {
}
// Api Method Type Constants
class APIMethodType {
}
// API Context that contain info for the API endpoint
class APIContext {
}
?>
companylibrary.php:
<?php
/**
* #package Company Library
* #author Company < hello#company.com >
* #version 1.0.0
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
define('WC_CL_VERSION', '1.0.0');
require_once plugin_dir_path(__FILE__) . 'vendor/autoload.php';
/**
* Classes are correctly autoloaded if the autoload directive for the
* package the 'root' package depends on, is specified at the 'root'
* package level.
*
* If the autoload directive is specified at the package level of the
* 'sub' level of the package itself, no classmaps are generated.
*/
$public_key("othercompanypublickey");
$context = new APIContext();
$context->set_api_key('companyapikey');
$context->set_public_key($public_key);
$request = new APIRequest($context);
composer.json (which does generate classmaps for api.php):
{
"name": "company/company-library",
"description": "A useful library",
"type": "library",
"license": "GPL-3.0-or-later",
"authors": [
{
"name": "company",
"email": "hello#company.com"
}
],
"require": {
"othercompany/otherlibrary": "^1.0"
},
"config": {
"optimize-autoloader": true
},
"autoload": {
"psr-4": {
"Company\\CompanyLib\\": "src/"
},
"classmap": ["vendor/othercompany/otherlibrary/api.php"]
},
"minimum-stability": "dev",
"repositories": [
{
"type": "package",
"package": {
"name": "othercompany/otherlibrary",
"type": "library",
"description": "Eases the integration of othercompany APIs into company's useful library",
"homepage": "https://otherlibraryportal.othercompany.com",
"version": "1.0",
"dist": {
"url": "../../../../repository/othercompany-otherlibrary-1.2.zip",
"type": "zip"
},
"require": {
"php": ">=5.6.1",
"phpseclib/phpseclib": "3.0.18"
},
"minimum-stability": "dev"
}
}
]
}

Do not compress the folder containing the othercompany/otherlibrary files but instead, simply compress the files.
For a working project, please refer to GitHub:
github.com/ezoer/companylibrary

Related

how to stop composer from removing manually installed package

For some reason I'm unable to install Predis package in my project via composer require predis/predis, I have manually downloaded Predis package from https://php-download.com and moved predis folder to the vendor and then updated files inside vendor/composer, it works fine.
However, the problem is when I run composer dump-autoload, this command removes all references of this package from vendor/composer/autoload_psr4.php and autoload_static.php files.
Can someone please help me how can I prevent composer dump-autoload to remove references of this package?
Instead of copying your package to the vendor directory, you can use "repositories" in install a local package:
{
"repositories": [
{
"type": "path",
"url": "../../packages/my-package"
}
],
"require": {
"my/package": "*"
}
}
I fixed it by adding following code in vendor/composer/installed.json , I forgot to add it.
{
"name": "predis/predis",
"version": "v1.1.1",
"version_normalized": "1.1.1.0",
"source": {
"type": "git",
"url": "https://github.com/nrk/predis.git",
"reference": "f0210e38881631afeafb56ab43405a92cafd9fd1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nrk/predis/zipball/f0210e38881631afeafb56ab43405a92cafd9fd1",
"reference": "f0210e38881631afeafb56ab43405a92cafd9fd1",
"shasum": ""
},
"require": {
"php": ">=5.3.9"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
},
"suggest": {
"ext-curl": "Allows access to Webdis when paired with phpiredis",
"ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol"
},
"time": "2016-06-16T16:22:20+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"Predis\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Daniele Alessandri",
"email": "suppakilla#gmail.com",
"homepage": "http://clorophilla.net"
}
],
"description": "Flexible and feature-complete Redis client for PHP and HHVM",
"homepage": "http://github.com/nrk/predis",
"keywords": [
"nosql",
"predis",
"redis"
]
}

How to add a package to a custom laravel package?

I'm building a custom Laravel package which requires the guzzlehttp/guzzle package. Below is my composer.json file:
{
"name": "lomse/awesomePackage",
"description": "this an awesome package",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Selom",
"email": "awesome#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4": {
"Lomse\\AwesomePackage\\": "src/"
}
}
}
Below is the content of my AwesomeProvider.php file:
<?php
namespace Lomse\AwesomePackage;
use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
class AwesomeProvider extends ServiceProvider
{
public function boot(){
}
public function register()
{
$this->app->singleton(Awesome::class, function ($app) {
return new Awesome(new Client); //Class 'GuzzleHttp\Client' not found
});
}
}
I keep getting Class 'GuzzleHttp\Client' not found.
What am I doing wrong?
So, this turns out to be quite simple. I highlighted the steps to take in order to solve this. Hope this helps anyone who is having the same issue.
I had to push my code to a repo lomse/awesome-package on Github
then specified preferred-install as dist in the ./lomse/awesome-package/package.json config property`:
"config": {
"preferred-install": "dist"
}
The full code is
{
"name": "lomse/awesome-package",
"description": "this an awesome package",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Selom",
"email": "awesome#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4": {
"Lomse\\AwesomePackage\\": "src/"
}
},
"config": {
"preferred-install": "dist"
}
}
In the root package.json, specify the repository of your package as follow:
"repositories": [
{
"type": "git",
"url": "git#github.com:lomse/awesome-package.git"
}
]
Also add your package repo to the package.json require property as shown below:
"lomse/awesome-package": "dev-master"
From your root directory, run the code below to update your dependencies. This will clone the lomse/awesome-package repo into your vendor folder and install any other dependencies required by your package:
composer update -vvv
-vvv is for debugging purposes

laravel 5.5 package developing need to use composer require vendor_name/package_name

I developed a Laravel 5.5 package with package auto discovery and pushed it at git hub
https://github.com/adamibrahim/authconfirm
when i run
$ composer require "adamibrahim/authconfirm" : "v0.1.1"
I got an error
could not find package adamibrahim/authconfirm at any version for your min ...
do i need to register my repository somewhere so i can use composer require command ?
here is my package composer.json
{
"name": "adamibrahim/authconfirm",
"type": "library",
"description": ":Laravel 5.5 Auth modifications to confirm the auth email",
"keywords": [
"Laravel5.5",
"Auth",
],
"homepage": "https://github.com/adamibrahim/authconfirm",
"license": "MIT",
"authors": [
{
"name": ":Adam Ibrahim",
"email": ":adamibrahim1701#gmail.com",
"homepage": ":author_website",
"role": "Developer"
}
],
"require": {
"illuminate/support": "~5.1",
"php" : "~5.6|~7.0"
},
"require-dev": {
"phpunit/phpunit" : ">=5.4.3",
"squizlabs/php_codesniffer": "^2.3"
},
"autoload": {
"psr-4": {
"Adamibrahim\\Authconfirm\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Adamibrahim\\Authconfirm\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"config": {
"sort-packages": true
}
}
Well, it's not enough just to create Github repository to use it via composer. You should create account at https://packagist.org/ and add your package in there to make it available via composer require.
In addition you should setup Packagist integration on Github at:
https://github.com/adamibrahim/authconfirm/settings/installations
to make sure after changes in Github Packagist will see those changes.

composer installs own TYPO3 extension in wrong path

I have a own TYPO3 extension hosted on bitbucket. Getting this via composer works (see here for input). The extension is downloaded into my vendor folder. Being there i cannot install the extension via extension-manager.
How can I getting my ext into typo3conf/ext (ensuring autoloading will work)?
The extensions coming via
{
"type": "composer",
"url": "http://composer.typo3.org/"
}
are going to (as expected):
web/typo3config/ext
here is my project composer.json:
{
"repositories": [
{
"type": "composer",
"url": "http://composer.typo3.org/"
},
{
"type": "package",
"package": {
"name": "metaxos/exaibbrplus",
"version": "dev-2016",
"source": {
"url": "https://metaxos#bitbucket.org/metaxos/exaibbrplus.git",
"type": "git",
"reference": "release/2016"
}
}
}
],
"name": "Metaxos/ibbrating2016",
"require": {
"typo3/cms": "7.6.2",
"bk2k/bootstrap-package" : "dev-master",
"typo3-ter/compatibility6" : "7.6.0",
"typo3-ter/extension-builder" : "7.6.0",
"metaxos/exaibbrplus": "dev-2016"
},
"extra": {
"typo3/cms": {
"cms-package-dir": "{$vendor-dir}/typo3/cms",
"web-dir": "web"
}
}
}
here is my extension composer.json:
{
"name": "metaxos/exaibbrplus",
"description": "custom ext for typo3",
"type": "typo3-cms-extension",
"version": "0.0.1",
"require": {
"typo3/cms-core": ">=7.6.0,<8.0"
},
"replace": {
"extkey": "self.version",
"typo3-ter/extkey": "self.version"
},
"autoload": {
"psr-4": {
"Metaxos\\Exaibbrplus\\": "Classes/"
}
},
"keywords": ["custom", "ext"],
"homepage": "http://www.blah.ch"
}
For Composer to install the package in web/typo3conf/ext, the package needs to have the type typo3-cms-extension. In your extension's composer.json this type is actually declared, however Composer will not respect it because you explicitly declare the package configuration in your project-level composer.json:
"repositories": [
# ...
{
"type": "package",
"package": {
"name": "metaxos/exaibbrplus",
"version": "dev-2016",
"source": {
"url": "https://metaxos#bitbucket.org/metaxos/exaibbrplus.git",
"type": "git",
"reference": "release/2016"
}
}
}
]
As you're using "type": "package" for your own repository, I suspect that Composer ignores the composer.json from that package. As you already have a Git repository that contains a composer.json, I'd suggest adding the repository using the vcs type instead:
"repositories": [
{
"type": "vcs",
"url": "https://metaxos#bitbucket.org/metaxos/exaibbrplus.git"
}
]
When doing that, Composer should use the composer.json from that repository, recognize the correct package type (typo3-cms-extension) and install the package in the correct directory.
You need to add the type key with the value typo3-cms-extension to the root composer.json. This will place your extension in web/typo3conf/ext instead of vendor/$vendor/$package which in turn will make it available to the cms.
It is important, to know that if you redefine the package in the root composer.json as repository type package, nothing from the extensions' composer.json file will be taken into account and you need to define any concerns about that package in the root composer.json.
So-applying the rules I mentioned above, your root composer.json will look like that:
{
"repositories": [
{
"type": "composer",
"url": "http://composer.typo3.org/"
},
{
"type": "package",
"package": {
"name": "metaxos/exaibbrplus",
"version": "dev-2016",
"type": "typo3-cms-extension",
"source": {
"url": "https://metaxos#bitbucket.org/metaxos/exaibbrplus.git",
"type": "git",
"reference": "release/2016"
},
"autoload": {
"psr-4": {
"Metaxos\\Exaibbrplus\\": "Classes/"
}
},
}
}
],
"name": "Metaxos/ibbrating2016",
"require": {
"typo3/cms": "7.6.2",
"bk2k/bootstrap-package" : "dev-master",
"typo3-ter/compatibility6" : "7.6.0",
"typo3-ter/extension-builder" : "7.6.0",
"metaxos/exaibbrplus": "dev-2016"
},
"extra": {
"typo3/cms": {
"cms-package-dir": "{$vendor-dir}/typo3/cms",
"web-dir": "web"
}
}
}
As mentioned above by #helmbert, you are either left with completely redefining the package or using another repository type. You may want to consider using satis or a URL repository.
try to add
"replace": {
"exaibbrplus": "self.version",
"typo3-ter/exaibbrplus": "self.version"
},
And use only "Metaxos\\Exaibbrplus\\": "Classes/" in the autoloader.

Add multiple library in composer.json

Actually I have tried this but it doesn't work. I'd like to preserve authors and descriptions. Of course I can do without. However I would like to know how to make this possible.
{
"name": "masterexploder/PHPThumb",
"type": "library",
"description": "A library for manipulating images in PHP.",
"homepage": "https://github.com/masterexploder/PHPThumb",
"keywords": ["image", "resize", "rotate"],
"require": {
"php": ">=5.3.0",
"symfony/filesystem": "2.2.*"
},
"authors": [
{
"name": "Ian Selby",
"email": "ian#gxdlabs.com"
}
],
"autoload": {
"psr-0": {
"PHPThumb": "src",
"PHPThumb\\Tests": "tests"
}
}
},
{
"name": "alexshelkov/simpleacl",
"type": "library",
"description": "Simple Access Control List (ACL) for PHP.",
"keywords": ["ACL", "authorization", "permission"],
"homepage": "https://github.com/alexshelkov/SimpleAcl",
"require": {
"php": ">=5.3.0"
},
"authors": [
{
"name": "Alex Shelkovskiy",
"email": "alexshelkov#gmail.com",
"role": "Developper"
}
],
"autoload": {
"psr-0": {
"SimpleAcl": "SimpleAcl/"
}
}
}
Composer encourages one library per package, so in your case you'd be better making two distinct packages for your two distinct libraries.
Also, see https://github.com/composer/composer/issues/1875
There is a workaround where you create a "master" package that "requires" your two sub-packages.

Resources