Loading src package in composer - php

So i have a composer package i am developing and i want to unit test the package without creating a new composer package for actually unit testing it. Is there any way to load the files in the autoload_classmap.php so they are accessible without creating a new package and including it there and then testing it?
For when i just have one package the files in the src folder is not load in the autoload_classmap.php at the path vendor/composer
What is strange here is that when i include the package in another repo it loads all the files correctly into the autoload_classmap.php file.
The autoload_psr4 package looks like this :
'Nyranith\' => array($baseDir . '/src')
But there is still no files from the $basedir in the autoload_classmap.php.
On of the classes is like this:
namespace Nyranith\Models;
trait CompositPrimaryKey
{
the path to this file is: src/Models/CompositPrimaryKey.php
The autoload in the composer.json looks like this:
"autoload": {
"psr-4": { "Nyranith\\": "src/"
}
},

In case of psr4, composer just uses path's listened in autoload_psr4.php to load all files from specified folder. So there is no need to have those classes in autoload_classmap.php
If you have already configured your composer.json for psr4 files, then just include vendor/autoload.php in the front controller of your application (usually index.php)

Related

Source from vendor loaded and compiled as files from src directory

I am trying the new Symfony4 (framework bundle) and I wanted to move some my reusable classes from src directory to vendor one (loaded via composer PSR-4).
So I created a package (new git repository), added classes there (into src directory), added PSR-4 configuration into package's composer.json like this:
"autoload": {
"psr-4": { "\\": "src/" }
}
and I ran composer require <name_of_my_package> and I dumped autoload.
All went smooth, but when I wanted to access any class from my package - for my surprise - Symfony cannot find and use any of these classes (even though they are in autoload files of composer).
I think that I need to somehow add these classes to Symfony container builder, but I am not sure how.
Thanks for all help.

Composer.json to autoload classes

So I was following this tutorial on how to autoload classes, the guy teaching it already had Composer installed, he went ahead and created a composer.json file in his project directory with the following content:
{
"autoload": {
"classmap": [
"./"
]
}
}
which supposedly autoloads any classes within the main project directory. And i cannot do this because if if i create a composer.json that is located anywhere but inside located in C:/Users/Mypc, the composer.json file cannot be found, i cannot install it in command line wich is the tutorial's step, composer install creates his autoloading. My autoloader cannot seem to find any classes inside any path I create in the class map, be it absolute or relative, I also managed to install and move composer file to usr/local/bin/composer but that's all I can do.
I can't create an autoloader no matter what I do and I also can't move the composer.json to my project directory inside xampp/htdocs/myproject, because if i do so i can't composer install since composer.json can't be found. I am using xampp.
So i finaly found out what the issue was.
I simply had to use the change director with cd C:\whateverpath\totheprojectdir and then install the composer on whatever the project was on.

Custom composer autoload directory, loading multiple packages

Im developing a group of packag within a Laravel 5 app.
all the packages have the following structure.
Folder structure:
Vendor/Package/src
composer.json
"autoload": {
"psr-4": {
"Vendor\\Package\\": "src/"
}
},
I would like to be able to have a separate folder outside of the vendors/ folder that contains the packages, under development. As they are the basis for the application. NOTE: These packages will not be downloaded through composer, just auto-loaded with composer.
e.g.
\Laravel-App
\app
\bootstrap
\config
\database
\public
...
\src
The \src directory containing all the packages mentioned above.
It's not relevant that there is a composer.json inside each package, because the are not managed by composer (, yet). Their autoload definition will only be used for an autoload dump by Composer, if the package is installed/updated.
Here there are not handled by Composer, just sitting around in a folder.
There are multiple ways of solving the autoloading problem.
You could:
use the PHP autoload functions to register your own autoloader (during bootstrap)
use Laravel's Autoloader to add the path to these packages (during app bootstrap, somewhere after laravel autoloader init)
use Composer's Autoloader to add the paths to these packages (during app bootstrap, after Composer init)
but the easiest way would be to define the mapping in the autoload section of the composer.json file of your main application. Then they will be included in the dumped autoloader, after install or update of your Composer managed dependencies.

Where exactly does laravel require the illuminate folder?

Looking at the laravel composer.json it seems to autoload the app directory but not the laravel illuminate framework.
It is listed as a require in the composer file. So ok, you can do a composer install and it will pull in the framework to the vendor directory. But where does the laravel app require the illuminate framework now for usage? Maybe I'm lacking composer knowledge here but I can't figure it out.
Looking at the illuminate environment it seems to come with it's own composer file that autoloads its entire folder.
I'm trying to look at the laravel structure because I'm currently working on a little project of my own with a src directory and an app directory but I can't seem to autoload both folder with something like this:
{
"autoload": {
"psr-4": {
"Cinematix\\": "src",
"App\\": "app"
}
}
}
Should this be able to work? If not, how would I implement something like this? Make a php package of my src folder myself?
Composer creates vendor/autoload.php which is then required by Laravel in bootstrap/autoload.php.
The way composer works is when you update or install a package it will regenerate that file by scanning each of the packages composer.json files, so theres conveniently just 1 file you need to include in your project to load all of your dependencies.
As for your own package, what you have there should work. Have you run composer dump-autoload after updating your composer.json?

How can I autoload classes with composer where the filenames are suffixed with .class.php?

I am using composer autoloader for my classes too, but my problem is that files with my classes follow the pattern ClassName.class.php and composer can load files only with .php extension.
Is there any way how to define file pattern in composer.json? I was checking classmap, but it doesn't support patterns.
Classmap autoloading should be your friend, see https://getcomposer.org/doc/04-schema.md#classmap.
Just specify the path to wherever you've got your files, let's say when your files reside in src, for example, src/Foo/Bar.class.php, then update your composer.json like this
{
"autoload": {
"classmap": [
"src/"
]
}
}
You will need to regenerate the classmap, though, every time you add a new file:
$ composer dump-autoload

Categories