Disable Composer require with php

Asked

Viewed 83 times

2

I have an example of JSON:

{
    "name": "xxxxxx",
    "description": "xxxxx",
    "type": "beta",
    "license": "MITS",
    "authors": 
    [
        {
            "name": "Leonardo Vilarinho",
            "email": "[email protected]"
        }
    ],
    "require": 
    {
        "php": ">=5.5.12",
        "twig/twig" : "*",
        "respect/validation" : "*"
    },
    "require-dev" : 
    {
        "phpunit/phpunit" : "*"
    },
    "config": 
    {
        "vendor-dir": "libs"
    }
}

I’ll probably have more libs in my project, I want to know if there is how I disable and activate these libs by PHP? If so, how can I do that? (I’m layman when the subject is composer, so I couldn’t even try anything, the research didn’t get me anywhere).

Although they are downloaded in the project are not loaded in the autoload of composer.

  • I don’t quite get it, but I think what it is is to remove the time, remove the ones you don’t want, something like: "require": 
 {
 "php": ">=5.5.12",
 "twig/twig" : "*"
 } and use the command php composer.phar update (or if it is composer update), when you want to activate again just reset and "require": 
 {
 "php": ">=5.5.12",
 "twig/twig" : "*",
 "respect/validation" : "*"
 } and use the command php composer.phar update

  • Let me give you an update on the question, the goal is to do in the PHP language itself, example, I have 3 dependencies, but I do not use one, so is there any way to cancel it in the autoload of Composer by the php language? Without removing and adding it via Poser

  • I get it, but they don’t charge if you don’t use them, I’ll add an answer

1 answer

2


The Poser-autoloader uses the spl_autoload_register, the classes that are in the "require": are only loaded if used new, or static calls Exemplo::teste(); run the includes, it works like this:

If you do this to the class Foo\Bar\Baz nay is loaded:

<?php
use Foo\Bar\Baz;

require_once 'vendor/autoload.php';

If you do this to the class Foo\Bar\Baz is loaded, the moment it uses new:

<?php
use Foo\Bar\Baz;

require_once 'vendor/autoload.php';

//Essa linha dispara o spl_autoload e inclui o ./src/Foo/Bar/Baz.php
$baz = new Baz;

Or:

<?php
require_once 'vendor/autoload.php';

//Essa linha dispara o spl_autoload e inclui o ./src/Foo/Bar/Baz.php
$baz = new Foo\Bar\Baz;

If you do this to the class Foo\Bar\Baz is loaded at the moment it calls the method exemplo:

<?php
use Foo\Bar\Baz;

require_once 'vendor/autoload.php';

//Essa linha dispara o spl_autoload e inclui o ./src/Foo/Bar/Baz.php
Baz::exemplo();

Extending classes also triggers autoload, even if you don’t use the child class:

<?php
use Foo\Bar\Baz;

require_once 'vendor/autoload.php';

//Essa linha dispara o spl_autoload e inclui o ./src/Foo/Bar/Baz.php
class Exemplo extends Baz {
}

How does the spl_autoload_register

  • It is only executed if the class does not yet exist
  • If you use the use does not shoot or include anything, the use serves more to create the aliases (nicknames)

I recommend you read:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.