How to define the path to autoload.php with composer - composer-php

I have a composer project, call it MyVendor\Database. That project uses composer and autoloading. There are several files in that project that have:
require_once __DIR__."/../vendor/autoload.php";
in order to utilize autoloading.
It all works great, until I include MyVendor\Database in another project.
I have another project, call it MyVendor\CoolPackage that requires MyVendor\Database.
I run composer install on CoolPackage and it downloads MyVendor\Database into the vendor folder.
The problem from here though, is My files within vendor\MyVendor\Database fail because they call require_once __DIR__."/../vendor/autoload.php", but there is not vendor folder inside of \vendor\MyVendor\Database.

In your composer.json you define the autoloading scheme. For reasons you found out yourself now, it is not a good idea to call the Composer autoloading inside any of the files that are supposed to be autoloadable not only inside that application or module, but in any other application as well.
Basically, the files in any Composer project should be divided based on the definition of PSR-0 or 4 autoloading: They either ONLY do declaration of classes or functions WITHOUT any side effects (like executing code), or they execute code without defining classes or functions. Only the former files can be autoloaded (with the exception of functions, which would only be able to always be declared by loading the file containing them with a files autoload section), the latter should NOT be autoloaded.
And then it should become relatively easy: All the classes that can be autoloaded can also be used when this component is included in another application. Anything else that is not a class cannot directly be used.

Related

Optional sub-package in a Composer package?

I have a Composer package which contains a test-suite.
The test-suite has some components that are only useful for testing, and these get autoloaded via require-dev to make sure you don't access them as a consumer of the package.
Now, it turns out, there are some other packages that need to use some of these components for their test-suites after all.
What I don't want to do, is simply make these components autoload by moving them from require-dev to require, because, again, these are components that we should only depend on for tests.
I also don't want to move these test-components to a separate package, as I want them versioned together with the package they belong to.
What I'd like to do is add a sub-package of some sort.
This would contain autoload configuration for these test-components, so that e.g. acme/foo includes a sub-package acme/foo-test that other packages (and the package itself) can explicitly add to their require-dev - making it possible to share these test-dependencies with other packages for testing only, e.g. without polluting the autoloader in production environments.
Is it possible for a package to define sub-packages that can then be installed from other packages?
Edit: I also don't want to use a "hack", like actually adding autoload rules to other packages like "Acme\\Foo\\Test\\": "vendor/acme/foo/src/Test". (Not even sure if that would work.)

Package manager - how to extend module with ability of module update

When I use some package manager like Composer I want to use some 3rd-party module and to have an ability for example to change some views (templates) of this module, and simultaneously to have an ability to update this module trough composer.
How can I do this?
If not - may be it is question to think about to integrate to package managers some pattern to have an ability do this well ?
Pattern - I mean some rules with that programmers will be able to show some files that will be able to be edited by consumer and to do it, developer should do some api for it by some standartized method that will be written in rules, f.e. in some composer config of his module developer will write, what files should be duplicated to userpath. After install, these files will be copied and user will be able to change it, and composer will do it able modules to see these files in users folder. And after update this user folder will not rewrite these edited files... Something like that or maybe somehow more flexibly
How to modify views of packages installed by composer in Laravel:
https://laravel.com/docs/5.0/packages#views
Customized views should be placed in folder /resources/views/vendor/
with same names as original
This is the first place where views are searched by framework

laravel 5, which files to put in production after a composer dump autoload

recently I added somethings into my composer.json file and did a "composer dump autoload". All work fine on development side.
When I put my updates in production, I had this error :
[2016-02-17 08:17:47] local.ERROR: exception
'Symfony\Component\Debug\Exception\FatalErrorException' with message
'Call to undefined function App\Http\Controllers\f_array_classe()' in
/home/dominiquir/ecole/app/Http/Controllers/MatieresEnseigneesController.php:67
when I put my app in production, I put only the "app" folder. But certainly when this file is modified , I must also put in production some other files, but which ones ?
An application which uses Composer has to put online
All files needed to run the application itself - you should know the location of the files because I suppose you created them
all files in the vendor folder.
If you don't know exactly point 1, the globally correct answer is: Your entire project folder after you ran composer install.
To optimize upload times, you should omit the development dependencies: Run composer install --no-dev to remove anything that is not being used for running the application. Development dependencies usually includes test frameworks like PHPUnit or any libraries that are only being used to help developing, like some command line interfaces for the framework being used etc. If your composer.json has entries under require-dev, that's what gets omitted.
The next step to reduce upload size would be to not upload anything of the application itself that is not relevant, like it's tests. But this depends entirely on how you constructed the application.
I would recommend to write a script that does all the preparations to create an optimized upload. It can be a simple shell script that creates a new, empty folder, then copies all needed files and folders into it, then runs composer install --no-dev there, and maybe then again deletes files from the dependency packages that are not needed (like test folders).
You may also run optimizing software for images, CSS and Javascript then. Most of them can be compressed in size without any loss of quality or function.
In the end you get a folder full of files you know have to go online. Just upload the whole folder. Done.
More optimization would be to try using rsync, which will compare the files already present at the target with the ones at the source, and only transfer the necessary minimum, but this requires SSH access of some kind, not just FTP.

How can I overcome Golang's requirement that every package have buildable code in it?

I am building a web application in Go and as part of it I have several middelware functions defined. Right now they all live in "my/middleware" package. That namespace is becoming very cluttered by all the different functions I've defined so I decided to put them all in their own subdirectories, e.g. "my/middleware/gzip". When I do this I get the error:
no buildable Go source files my/middleware
I don't want all of these functions in the same namespace, but it seems my only option is to create a placeholder .go file in the my/middleware directory with an empty init function or something. That sounds terrible so I'd like suggestions on how to achieve my goal to group a similar class of packages when there isn't any shared/common code to live in the parent package.
You are actually taking the right decision by splitting the files into different subfolders. It is not different than what is done here
https://golang.org/pkg/compress/
This allows for the clients of your framework to take only what they need. The idea is to avoid dependency bloating. Go is all about being lean.
The error you receive is because you try to build a package that doesn't exist. Think of that folder as a logical grouping mechanism, you need to build the packages given by the child folders individually.

How can I override a local module in Magento?

There are many examples of how to override a core module in Magento, but how does one override a local module?
You don't override a module, you override (or, more properly, rewrite, see below) a class that belongs to a module, or belongs to the general lib folder.
Based on your wording, I assume by override you mean "taking a class from app/code/core/" and placing it in "app/code/local/"? If that's the case then you mostly can't. Code pools work because magento adds four paths to the PHP include path.
app/code/local
app/code/community
app/code/core
lib
Then, when the autoloader says
include('Mage/Module/Path/To/File.php');
if will first check the local folder, then the community folder, then the core folder, and finally the lib folder. The local folder always wins.
If your class is a Model, Helper, or Block, I'd suggest using the module based rewrite system. The same techniques you use to rewrite classes in the Core modules can be used to rewrite classes in local or community modules. The point of a module based system is that the code which runs the core system is inserted / behaves the same way as code other people might add to the system.

Resources