include php package in file instead of composer - php

I want to include a php package, a css parser: https://github.com/sabberworm/PHP-CSS-Parser In installation guide it says to "Add php-css-parser to your composer.json":
{
"require": {
"sabberworm/php-css-parser": "*"
}
}
(I don't even know what composer.json is) Since I need only in one script, is there a way to include it only in a file? Like:
<?php
include //something here
//Do stuff with css parser
?>
Thank you

Your best approach would be to familiarize yourself with composer, what it's for and how it works.
You could copy the code from the module manually and include it in your project, but why bother?
You should simply need to run composer require sabberworm/php-css-parser from your project's root directory. Composer will automatically create the composer.json file which defines your project information and its dependencies, and the composer.lock file which stores information about the version of each dependency you've installed so you can deploy specific versions to other places.
If you don't have composer installed, check out the download instructions.
Once you've installed it, you need to include the composer autoloader and you will be ready to include your custom package:
<?php
// Include composer's autoloader
include 'vendor/autoload.php';
// Now you can use any composer installed package:
$parser = new Sabberworm\CSS\Parser($sText);

Related

Fatal error: Interface 'Coinbase\Wallet\Authentication\Authentication' not found

I am trying to coinbase PHP API, I have downloaded PHP library from GitHub and created an index.php file to start working, below is index codes
<?php
require_once('src/Client.php');
require_once('src/Configuration.php');
require_once('src/Authentication/ApiKeyAuthentication.php');
use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
$apiKey="";
$apiSecret="";
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);
?>
And this is generating below error
Fatal error: Interface 'Coinbase\Wallet\Authentication\Authentication' not found in /home/exhakduz/api/coinbase-php-master/src/Authentication/ApiKeyAuthentication.php on line 8
I can't find any solution
Use composer instead, and require the composer autoloader within your index.php. The docs suggest to install the library with composer as well.
Install the library using Composer. Please read the Composer
Documentation if you are unfamiliar with Composer or dependency
managers in general.
Composer setup / installation
Note: All commands below need to be run from the same directory where your index.php is located.
First off you'll need to download and install composer. The current version available is 1.8.6. Download this phar to the same location as your index.php script. Also create a composer.json file with {} as the contents, composer will save your dependencies to this file.
Make sure composer.phar has execute permissions (if on linux run chmod +x ./composer.phar)
Run ./composer.phar require coinbase/coinbase. This should install the dependencies within a vendor directory.
Finally, you can require the autoloader composer generates when installing dependencies, and the missing Interface error you are seeing will be resolved.
The composer.json file should contain the following (bare minimum):
{
"require": {
"coinbase/coinbase": "^2.8"
}
}
Example using autoloader
<?php
require_once('vendor/autoload.php');
use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
$apiKey="";
$apiSecret="";
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);

Laravel 5.1 Package Development - loading packages dependencies in development

Hi I'm trying to develop a package in Laravel 5.1. Thanks to help here I have the basics set up.
My current problem is how to load dependencies for the package while I'm developing it.
In the packages composer.json I have added dependencies and have these installed now in a vendor folder within my packages development folder. This is not the frameworks root vendor folder.
Here's my require section of the packages composer.json:
"require": {
"illuminate/support": "~5.1",
"php" : ">=5.3.0",
"google/apiclient": "dev-master"
},
Because they are not part of the main autoload process what is the best approach to ensuring the dependencies for my package are loaded correctly from within the development folder? How do I include the autoload? I'm concerned that if I reference them to their current location/namespace that it will break when later installed as a package in another app.
in my code I have the following:
$client = new \Google_Client($config);
which gives the error:
Class 'Google_Client' not found
I can get round this by adding this dependency to the main composer.json but don't think that is the correct approach to keep the package development independent (if that makes sense)
When I developed in L4.2 there was the workbench which took care of the loading which of course no longer features in L5.1
Any help and best practice appreciated
Because they are not part of the main autoload process
I think you misunderstood how composer dependencies are managed. When in your main compose.json file you list a dependency, composer will add it to the main autoload process as well as all their dependencies, and the dependencies of their dependencies, and so on recursively.
You don't have to worry about where the dependencies are stored or how Composer will load them. Composer will automatically add them to the autoload file and all you have to do is make sure you require the composer autoload file. Once you require the composer autoload file, all the classes and functions loaded by composer will be available. Provided you required the composer autoload file all you have to do to use the classes from any of the installed packages is to make sure you address them using the proper namespace. Composer is smart enough to know where all classes are stored and how to load them (that is what psr-0, psr-4,... are for).
So if you are developing a Composer package, lets call it 'A', and you list the package 'C' as one of the dependencies of your package 'A', composer will add it to the autoload file for you. If you use another package, lets say, Laravel, which has a dependency of you package 'A', then also the package 'C' will be available within Laravel, since it is a dependency of 'A'.
I.e: If this is your composer.json file
{
"name": "foo/bar",
"require": {
"google/apiclient": "1.0.*"
}
}
This code will work
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);
Note I've required the composer autoload file, which seems to be your problem. When you are using Laravel, it will add that file for you.

Guzzle installation - Client Use

Forgive my ignorance but i've been struggling to get Guzzle to install in my project's folder.
I have installed Composer via it's .exe installer.
I have a composer.json file which contains the following:
{
"require": {
"guzzlehttp/guzzle": "~5.1"
} }
I can run 'composer update' in which it installs Guzzle.
However, it installs "Vendor" folder which has folders
'Guzzlehttp'-> 'guzzle' then is has 'build','src',tests' etc.
I want to use the Client object but the documentation says add:
'use GuzzleHttp\Client;' to the top of the PHP file but I don't actually have that file structure. Inside my guzzlehttp is just 'guzzle' or 'streams'folders.
Am I write in thinking Guzzle has not installed propperly? Or is there a different way to do it in Guzzle 5?
Many thanks for any help
You should include Composer's autoloader file which takes care of loading any Composer-installed library, the autoloader file is located in the vendor directory.
Use the following in your code :
// include Composer's autoloader
require "vendor/autoload.php";
// this should work just fine now
$resp = GuzzleHttp\get("http://stackoverflow.com");

Why do CodeIgniter projects sometimes include composer.phar package?

I'm new to Composer and in my current project I would like to install a bunch of PHP libraries like:
Doctrine
Security Library (Which i have no idea but looking for in CodeIgniter)
Bootstrap layout libraries and other when necessary
For that matter , I would like to use Composer based library management in my application,
and i get confused that if i have to include composer.phar on my project directory or not.
since i have it on my environment path and I can run Composer form command line .
How can integrate the above libraries into my codeigniter application then..
Appreciate your toughs!
The composer.phar file is an executable and it should not be committed. What it actually does is that it looks in your composer.json file and there you can declare some more dependencies (libraries for example) and their version:
{
"require": {
"doctrine/orm": "*"
}
}
The version in this case is declared with "*" so Composer will get the latest version. This is very useful if there are more people on the project, to make sure all of them have the same version of dependencies installed (so the composer.json file must be committed).
If you run "composer.phar update" on the other hand, this will get the latest version of all dependencies, no matter the version placed in composer.json and updates the lock file with the new versions.

Using PHP Slim Framework

I want to use Slim for PHP in my project for the first time.
The manual says:
Install composer in your project:
curl -s https://getcomposer.org/installer | php
Create a composer.json file in your project root:
{
"require": {
"slim/slim": "2.*"
}
}
Install via composer:
php composer.phar install
Add this line to your application’s index.php file:
<?php
require 'vendor/autoload.php';
I'm afraid, I don't get it. Where should the commands "curl" and "php" be used? I only access my webspace through Filezilla. How can I then apply such a command?
What do those steps do anyway? Sadly, the manual is not helpful at all.
See http://www.slimframework.com/install:
MANUAL INSTALL
Download and extract the Slim Framwork into your project directory and require it in your application’s index.php file. You’ll also need to register Slim’s autoloader.
<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
And there are links to zip-files.
If you're getting started on slim i'd definitely suggest that you get a good IDE that will guide you through the whole process. When I started the slim framework, I came across an IDE by jetbrains called PHPStorm. It makes everything so easy by doing most of the stuff you listed for you...
download and install PHPStorm https://www.jetbrains.com/phpstorm/download/
download and install Composer https://getcomposer.org/download/ so PHPStorm can use it.
get to the part where you start PHPStorm.
go to File > new Project > Composer Project and follow the motions.
It'll create all the files you listed. Then all you have to do is look and learn what it all means.
Composer is basically a package manager, you basically open a cmd and navigate to the place you want to create you PHP Slim application and type some composer commands to install package files in that folder. Composer then gets the packages and puts them in a directory called 'vendor' in that project folder of yours.
{
"require": {
"slim/slim": "2.*"
}
}
that's basically a config file that either you or composer will create in the same file also.

Categories