How do I include a composer package into plain php? - 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"
}
}

Related

Composer - Setup autoload in new package

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';

mPDF : Methods with the same name as their class will not be constructors in a future version of PHP; mPDF has a deprecated constructor

I'm new to mpdf and I'm having a problem implementing it on my project. I saw a link in Facebook that has a tutorial for mpdf, I read and followed all the instructions but unfortunately there is still an error about some deprecated value.
Here is the link I followed : http://codesquery.com/convert-html-pdf-codeiniter-using-mpdf/
And here is the screenshot of the problem:
I hope you guys can help me.
I guess you use php 7. You must change names of function. In old php for example 5.19 the same name of function and class means that this function is a constructor. In PHP 7 it must be for example:
public function __construct(mPDF $mpdf){}
Click here to see a list of all modification
You can easily install mpdf by using composer in codeigniter. This is how I implemented composer in CodeIgniter 3.It is very easy. You have to install composer on your machine download it from https://getcomposer.org/. After installing composer in your pc,
Copy and paste composer.json file in the project folder to application folder.
In the config.php file $config['composer_autoload'] = TRUE;
Now you have composer in your project. Now i will saw you how to install mpdf using composer
Open cmd and direct to application folder
Inside application directory Type composer require mpdf/mpdf
Now a vendor folder will be created inside application folder and inside vendor folder you can see all your packages downloaded by composer.
Now since you autoloaded composer now you can just use the code given by mpdf official manual like in your controllers.
function m_pdf(){
$mpdf = new mPDF();
// Write some HTML code:
$mpdf->WriteHTML('Hello World');
// Output a PDF file directly to the browser
$mpdf->Output();
}
Remember you don't need to type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php'; since you already autoloader composer. If not prefer to autoload composer you must type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php' at the beginning of each controllers where you use the mpdf vendor libraries. If you encountered any problems please comment below.

I have PHP code I need to use—how do I install it?

I'm trying to get some code set up to use an particular company's API.
I have experience with Perl and if I need a module installed, I type cpan ModuleName and most of the time it Just Works. How does that work with PHP code of similar complexity?
The company in question have a github repository with PHP Client system to access their API, which looks much the same as a perl Module.
I can git clone it, I can download it, but then what? Do I have to install it? There are no installation instructions. Or do I just start using it? There's a composer.json file in there. Do I need to run a composer command so it can figure out and install its dependencies like a CPAN module would? Will it install into system folders or just right there in whatever directory it happens to be in? I feel like there ought be some kind of official installation process because there's a /tests/ folder in the files I downloaded.
Their example code literally starts like this:
<?php
/* #var $CompanyName \CompanyName_Api */
$CompanyName = new \CompanyName_Api();
/* do interesting stuff */
and that's it. Of course nothing works if I just do that because it doesn't know where the CompanyName_Api files are. It works if I add this:
<?php
include('/full/path/to/downloaded/files/CompanyName/src/Api.php');
is that all I need to do?
In order to install all dependencies defined in composer.json you would run the following command inside the project directory:
composer install
This will find and download the dependencies into the vendor directory and it will also generate an optimized autoloader.
To autoload your own source files you'll need to add it to the autoload section in the composer.json file:
First your need to install an PHP environment like PHP, Apache and all stuff, then you need to clone that file from the git repository or just download it, then navigate to the dir and fire the command composer install. It will install all of the dependencies required for that package. After that, run the code from the browser -- the package api code may have the auto loader file which you need to include in your current package and autoloader will do all the stuff for you. Add your folder structure and file structure so that you get a better answer on this.

Reference .phar classes installed globally with composer

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

Where do I need to put file composer.json for composer to work with Symfony

I have installed composer from this link, but I still could not find where do I need to place the composer.json file.
In the root of your project. Because you mentioned symfony, just have a look at the composer.json from the symfony-standard edition
https://github.com/symfony/symfony-standard/
Or even better: Follow the official instructions and use symfony-standard as starting point for your project directly.
Start by visiting the Symfony2 download page at http://symfony.com/download. On this page, you'll see the Symfony Standard Edition, which is the main Symfony2 distribution.
You can forkt it directly from github.
I just found on other topic that you can use :
php composer.phar --working-dir=/home/user/folder/ update
You may need to create a file called 'composer.json'
See this document "https://getcomposer.org/doc/00-intro.md#declaring-dependencies"
You could create a composer.json file by using the composer init command whilst in the directory you want it to be created in
composer init
or
path/to/composer.phar init

Categories