php composer reuse of modify package - php

Using composer, I have come across a package that does what I want, if I modify the constant values and some of the code in private methods.
What I've been trying to do is create my own package and extend the other package that I've found and try to override the methods from within my own classes. However, as the package contains constants and private methods, it's difficult/impossible to implement my own package to take advantage of the other package.
What I'm wondering now is what is the best way to go about using the code in this other package? Do I copy the code into my own package and change the namespace and modify the code for my own needs? Or is there another way where I can change the values in another package and modify the private classes?

Related

Override class in "porto" architecture

I have the APIATO application. And install the library (some wrapper for 3rd part API) by composer.
I need to override classes to add some custom logic into these classes.
Where can I create overridden classes?
There are multiple ways to override the packages
1)Extends the class and override the function.
2)implements the class.
3)You have to create a file in the required container and same way you can use it(same like original usage of the package with overridden file path).

Should I copy PSR interfaces or add external depndency?

For example, I really like the Logger Interface by PHP Fig & want to use it as an interface in one of my packages.
I'd rather not add external dependencies (through composer), so I'm thinking about copy+pasting what they have into my own interface, which may have a couple of it's own functions as well.
Is there a good reason to use the package dependency to get the interface instead of copy+pasting the interface?

PHP: How to extend a 3rd party vendor class downloaded with composer and load it instead of the original?

I have a library downloaded with Composer which performs several basic stuff but which has very poor class methods in some cases.
So I though to create my own class (for certain objects, not all of them) which extend the library base class and add some useful method to it.
Unfortunately I'm not very familiar with Composer and autoloader.php.
How can I achieve that? I need to create my own library and run composer?
Two choices come to my mind, you can:
Fork the entire project and change as many things as you want
Extend the classes you need, and replace declarations/injections in your src code, there's no need to modify the autoloader.

php override vendor package class method

What is the proper way to change a package installed through composer in the vendor folder.
A: copy the package directory to the project source (so it is not changed by composer anymore) and use it from there.
B: Extend the class.
I would think that B is the proper way, but i see two problems with it.
1. The extended class may not be compatible with an updated baseclass from the vendor directory. (after "composer update")
2. How do I manipulate a method way down in the class chain.
$classA = new ClassA();
$classB = $classA->getsomeOtherClassInstance();
$classB->methodToChange();
I.e. I would have to change class A and B to make sure that the extended version of B is called.
The extended class may not be compatible with an updated baseclass from the vendor directory. (after "composer update")
The class should always be backwards compatible if you choose the correct version number in your composer.json file. Take a look at https://getcomposer.org/doc/articles/versions.md (Keep in mind this only applies for code repositories that promise backwards compatibility, but most big names do.)
Also, if you want you can choose a concrete version like for example '3.7.7' which would prevent composer update from updating your code, will always use that version. But better to do the above.
How do I manipulate a method way down in the class chain.
If it is a private method then you were not meant to change it, better to look for its caller and override that method and class instead. Also, take a look at Decorator design pattern it allows you to change behavior of methods dynamically on the fly.

How can I safely add to a composer package?

I am using the Composer package Omnipay in my project and I want to add a new class to the package (in my case it is support for a new payment gateway). The naming does not conflict with anything and it follows the same naming and structure conventions as sibling folders. However, when I run composer update it deletes my whole folder of changes even though it didn't need to. Is there a way I can tell composer not to delete that directory?
Leave everything under /vendor alone, don't change any files there. You should treat libraries as external dependencies - don't check them into source control or change them in any way. Just reference them in your own code.
If you need to customize a composer library, you can either work around it in your own code (most PHP libraries support dependency injection, which will let you override any of the libraries' classes), or alternatively you can fork the library on github, then reference your own fork in composer.json, or submit the pull request so everyone can benefit from it.
As far as I'm aware, you cannot add to a Composer package as it is external and therefore out of your control. You should treat the packages as libraries only, and add all classes into your own project, ensuring that the packages you need are still set up in your .json file

Categories