Including PHP Defines() using Composer - php

I'm using Composer for module dependency management (loving using autoload.php instead of a ton of includes and requires!).
I want to include a PHP file that is outside of the root Composer directory (for security) with credentials stored in defines().
This isn't working, composer.json:
{
"autoload": {
"classmap": ["../credentials.php"]
}
}
credentials.php:
define('RYAN','BRODIE');
test.php:
require_once __DIR__.'/../vendor/autoload.php';
echo RYAN;
Results in Notice: Use of undefined constant RYAN. If Composer's autoloader is only intended for Class includes then I'd be grateful for any hacks (as it were) to make this work.

That method should work fine, however you'll need to use files instead of classmap for example;
{
"autoload": {
"files": [ "../constants.php" ]
}
}

Related

How to use composer's autoload when I already use autoload?

I have a config.php file where I use autoload for all my personal classes
function __autoload($class_name) {
include __DIR__.'/classes/'.$class_name . '.php';
}
I need now to use a 3rd party class from composer which is located in
/vendor/guzzlehttp.
So my code is now:
require('Config.php'); // my config file: this is used in ALL site
require 'vendor/autoload.php'; // the copmoser
$client = new GuzzleHttp\Client([]); // call to the 3rd party class installed by composer
Which raises a 404 error : php searches GuzzleHttp in /classes
Uncaught Error: Class 'GuzzleHttp\Client' not found
I have no idea on how to solve that: I need to keep my own classes in /classes
I need to autoload them because all the website uses that.
So: how can I use classes installed by composer in my website?
My composer.json content is:
{
"require": {
"firebase/php-jwt": "^5.0"
}
}
Autoloaders can be stacked just fine, but you'll want a check in place to see if the file exists before trying to include it.
However, if you're using Composer already, just let Composer handle the autoloading.
Add a reference to your global namespace being loaded from the classes directory in your composer.json's autoload PSR-4 section:
"autoload": {
"psr-4": {
"": "classes"
}
},
__autoload() is deprecated and seems to be incompatible with current best practice, spl_autoload_register(), which is what Composer uses.
function myAutoload($class_name) {
include __DIR__.'/classes/'.$class_name . '.php';
}
spl_autoload_register('myAutoload');
But you should really look at moving your dependencies into that dependency manager that you already have, Composer.
For external libs that are already in Composer, like Guzzle: https://packagist.org/packages/guzzlehttp/guzzle
For internally-developed libs: https://getcomposer.org/doc/04-schema.md#autoload
Edit: on second look there's no reason #Devon's answer wouldn't work.

How to load a library from vendor folder symfony?

I have downloaded a library with composer. Now I want to require that file from vendor directory in my controller. So how do I require that file.
To be more specific I want to require this library
https://github.com/jumbojett/OpenID-Connect-PHP
Once composer is done downloading your libraries it generates the autoload namespaces for your app.
To check what the generated autoload namespace for a library will be just look at its composer.json file.
"autoload": {
"classmap": ["OpenIDConnectClient.php"]
}
and the generated autoload map will be in vendor/composer/autload_psr4.php
Here is the relevant part for your library.
Then open vendor/composer/autoload_namespaces and check what is the actual namespace generated by composer.
when composer.json has the 'classmap' key it basically means that you will access the librabry via '\LibraryName.phar' for example.
When it has the 'psr-4' key it means that you will access your library from the namespace specified there
Example:
"autoload": {
"psr-4": {
"Blast\\BaseEntitiesBundle\\": ""
}
},
You just need to require "vendor/autoload.php" all packages namespaces will be available for you to use any where.

ZF2: autoloading libraries without namespaces

Previously I have only been using third party libraries that use namespaces together with Zend Framework 2. Now I need to use a library that does not use namespaces, and I cannot seem to make it work. I installed it via Composer, and it is installed fine into the vendor directory. I am trying to use it as follows:
$obj = new \SEOstats();
The result is a fatal error indicating that the class could not be found. I have tried to manually configure the StandardAutoloader, but so far without any luck. I thought that the autoloading would be done for me automatically when installing through Composer, but I guess that is not the case without namespaces? I haven't seen any reference to the library in the autoload files that Composer generated. I guess I have to do it manually - but how?
Thanks in advance.
You can use the files and classmap keys.
As an example consider this composer.json:
{
"require": {
"vendor-example/non-psr0-libraries": "dev-master",
},
"autoload":{
"files": ["vendor/vendor-example/non-psr0-libraries/Library01.php"]
}
}
Using the files key you can then use
$lib = new \Library01();
Use the classmap key when you need to load multiple files containing classes. The composer.json would be:
{
"require": {
"vendor-example/non-psr0-libraries": "dev-master",
},
"autoload":{
"classmap": ["vendor/vendor-example/non-psr0-libraries/"]
}
}
Composer will scan for .php and .inc files inside the specified directory configuring the autoloader for each file/class.
For more info you can check http://getcomposer.org/doc/04-schema.md#files and http://getcomposer.org/doc/04-schema.md#classmap
If you are under a namespace while creating the object, you must use the "\" (root namespace), otherwise you will use the Library01 class under the current namespace (if you have one, if you don't have one you'll get an error).

Silex, loading own classes

I play around with Silex, PHP micro-framework. At the moment I try to load my own classes but I have no luck doing it. Maybe somebody could explain me a little how does loading in Silex works?
My project structure looks like this:
app/
vendor/
web/
tests/
bootstrap.php
composer.json
composer.lock
Let's say I want to load a class Controller\User (namespace here) from /app/MainController.php.
How can I do that? I've browsed some articles (loading via Composer or Symfony's UniversalClassLoader), followed some instructions, and still it does not work.
If someone could give me a hand with it please, I would appreciate it.
I assume you load your Silex classes in bootstrap.php like that:
// bootstrap.php
require_once __DIR__.'/vendor/autoload.php';
If so replace this code with the following:
// bootstrap.php
$loader = require __DIR__.'/vendor/autoload.php';
$loader->add(YOUR_NAMESPACE, DIRECTORY_OF_THE_NAMESPACE);
Example:
// bootstrap.php
$loader = require __DIR__.'/vendor/autoload.php';
$loader->add('Tutorial', __DIR__.'/src');
You can add multiple namespaces if you like, just call the add method for every namespace.
In src/ you would store your class files. For every namespace create a folder which contains your class files for this namespace.
The file should have the same name as the class.
MyNamespace\MyClass => src/MyNamespace/MyClass.php
MyNamespace\SubNamespace\SubClass => src/MyNamespace/SubNamespace/SubClass.php
In every class file you have to set the associated namespace in the first line.
// src/Tutorial/DemoController.php
namespace Tutorial;
class DemoController{
// class definition goes here..
}
You have now access to your classes in every file which includes bootstrap.php.
In /app/MainController.php you can now access your own class like this:
// app/MainController.php
use Tutorial\DemoController;
$foo = new DemoController();
This solution worked for me. Hope it works for you.
I was also looking for the same thing, since Silex documentation still has the registerNamespace function that was removed.
Found a really nice answer here
In Short, all you have to do, is add a "psr-0" in "autoload" section in the composer.json
Example composer.json:
{
"require": {
"silex/silex": "~1.3",
"doctrine/dbal": "~2.2"
},
"autoload": {
"psr-0": {
"MyApp": "src/"
}
}
}
Make sure to update the Composer Autoloader ("composer install" or "composer update")
As of October, 2014 the best way to autoload your own classes into silex, is using PSR-4 (instead of PSR-0) autoloading through Composer using composer.json file in your application root:
{
"require": {
"silex/silex": "~2.0",
},
"autoload": {
"psr-4": {"Vendor\\Namespace\\": "path/to/src/"}
}
}
After editing your composer.json you need to do a:
composer update
on your project root directory, using terminal.

add a library to silex

I know this question has already been asked, but it seems that autoloading process changed a little bit with composer.
I just want to add a class library to my silex project.
So I made this file:
vendor\lib\picture.php
<?php
namespace MyNamespace;
class Picture
{
function testage()
{
echo 'hihaaa ça marche'; exit;
}
}
in vendor/composer/autoload_namespaces.php, I added this line to the big array:
'MyNamespace' => $vendorDir . '/lib/',
And in the main file I added:
use MyNamespace\Picture as Picture;
and called it like that:
$app->register(new Picture());
which gives me this error:
Fatal error: Class 'MyNamespace\Picture' not found...
I just don't know how to add a class that I can use from any controller, easily, without command line (I don't use composer, I downloaded silex preconfigured), any idea?
If you're using composer you should not change the vendor directory. You should not add files into it, and you should not modify the composer-generated files.
I recommend you put those classes into the src directory. #gunnx shows how you can configure autoloading in composer.json, so that it gets re-generated every time you run composer install.
The file would be in src/MyNamespace/Picture.php. The autoload config in composer.json would be:
{
"autoload": {
"psr-0": { "MyNamespace": "src/" }
}
}
The actual solution is a combination of the two previous answers. But I think it's important to get the details right ;-).
Your Picture class should be in this file: vendor/lib/MyNamespace/Picture.php. Note the full namespace and the casing.
You can add your own code to the autoloader by adding the following to your composer.json
e.g.
{
"autoload": {
"psr-0": {"Acme": "src/"}
}

Categories