How to use git with MVC pattern - php

How to use git, when I have large tree of folders in file structure of web-project. Module, which I developing is separated by different folders in this file structure. As it is customary by MVC pattern concern.
In the overall file structure of web-project I have, roughly speaking:
model folder
controller folder
views folder
languages folder
and so on
I making changes in files at this folders and need track changes. These folders are not combined in one folder, that associated with module, that I developing. These folders are scattered in different parts of the file structure.
I could create git repository at the root of file structure and in .gitignore specify, which folders track. But I develop many modules. And I need separate git repositories for them.
Where and how create git repositories to developing many modules in large file structure?
If I init git repository at the root of web-project is it possible to create many repositories at the root of file structure for each module and for each repository specify which folders git should track?

I think simplest solution now days it's to keep your independent modules in separated repositories and then requesting them using composer.
I will show you some theoretical example of it.
I have project, which should use Payment Module. Payment Module is a separated repository with composer.json file provided in root directory.
For example:
{
"name": "company/payment-module",
"description": "Module handling payments from our customers.",
"autoload": {
"psr-4": {
"Company\\PaymentModule\\": "src/"
}
}
}
That gives you possibility of using any of modules created in any application/project you'll build.
So, for example in your project you can require Payment Module as dependency.
{
"name": "company/shop",
"type": "project",
"description": "The main repository of our shop.",
"autoload": {
"psr-4": {
"": "src/"
}
},
"repositories": [
{
"type": "git",
"url": "https://github.com/company/payment-module"
}
],
"require": {
"company/payment-module": "dev-master"
}
}
When you define modules you want in the project and install them using composer, they will be under vendor/ directory and will be autoloaded into the namespaces you define.

Related

Composer/Laravel install package from existing files when external repository no longer exists

I have the following problem: I have an old project which uses Laravel 4.2, PHP 5.5.9 and Composer. I'm trying to set it up on a different computer (With Laravel 4.2.2 and PHP 5.6) but one of the required packages has a dependency which is missing because whoever was managing that GitHub account decided to remove it. Thus, the required package can't be installed via Composer.
Now, the old project has these packages downloaded and I can copy both of them manually. What I don't know, is how to correctly add them to the project this way and stop composer from trying to download them anew.
No code examples give as none are needed.
If you have the files, and you won't be able to continue using composer to manage that package (e.g. for updates, removal, etc), then you just need to treat the files as if they belonged to your project.
It's hard to give you specifics without knowing more about the package you want to use, but a general approach would be:
Let's say that the package you want to use is funtastic/foobar. If you have the files from your old vendor, we just need the directory inside the vendor/funtastic, which might be called foobar.
First, copy your files within your project. Since they belong to a different namespace than the rest of your application, I personally wouldn't put them in src. You could put them in a new directory called lib, for example.
So now your file structure would be something like:
project-root-dir
├── public
│ └── index.php
├── vendor/
├── lib/
│ └── foobar/
│ └── some files ...
│ └── src/
├── composer.json
├── composer.lock
Now you need to check the package's composer.json, specifically the autoload section. It might say something like:
"autoload": {
"psr-4": { "Funtastic\\FooBar\\": "src" }
}
You now need to go to your application's composer.json, find the autoload section and edit it so it includes the Funtastic\FooBar namespace. Assuming you already had an App namespace:
"autoload": {
"psr-4": {
"App\\": "src/",
"Funtastic\\FooBar\\": "lib/foobar/src"
}
Additionally, you also need to check the require section of the original package and see if it depends on any package and add those packages to your application's "require" section in its composer.json.
With all this in place and the composer.json correctly edited, you could simply regenerate the autoloader (composer dump-autload) and you would be ready to go.
Obviously, since I don't know the specifics of your package and project, you'll have to adjust these instructions to your specific case, but it should not take long to have the package working within your application.
Alternatively, if you want to continue treating this package as if it were an "external" dependency:
Zip all the package files into package.zip and put it into base_dir/lib
Add the following to your composer.json:
"repositories": [
{
"type": "package",
"package": {
"name": "vendor/name",
"version": "1.0",
"dist": {
"url": "lib/package.zip",
"type": "zip"
}
}
}
],
(Solution originally proposed by the question author themselves).
With this the files will exist twice in your project: as a "repository", and installed on vendor, which I find less than ideal. You can also keep the files outside of your project, but that would require additional tracking.
I would personally bite the bullet an accept this package is no longer an external dependency, but files that should be maintained by the application maintainer.

What's the proper way to use SATIS with shared internal library

The problem:
I have Entities that are and will be the same for 2 projects written in Symfony. We got an idea to share them between projects. The first idea was to use git sub-module but everyone knows that it's not the most comfortable solution. So we put them in Satis as separate git repository.
In one project I would like to edit them in application directory src/AppBundle/Entity on the other they can be downloaded into vendor directory.
The question is how to setup composer so I can work with them not in vendor directory. How the commits will look like? Is git sub-module required for this?
I've already read about "type" : "path" for repository, I've checked composer installers. Is there any other solution than symlink which comes to my mind right now?
So to sum up.
How to work with shared library in one project from app directory and on the other on vendor directory?
This is the solution that worked for me.
I've cloned internal library into project.
In main project I've added this directory to .gitignore and in composer.json I've added following lines
"repositories": [
{
"type": "path",
"url": "internal-library",
"options": {
"symlink": true
}
},
{
"type": "composer",
"url": "http://our-satis"
}
],
In the other project I've added only satis repository(the entities will be changed only in one project and imported to the other).
"repositories": [
{
"type": "composer",
"url": "http://our-satis"
}
],
So now in development when I do composer update the library is symlinked to vendor directory. If I don't have this directory it will be get from satis. In production repository will be get from satis because my direc tory does not exist. I've had some issues with PSR loading but everything works as expected.
I hope it will help someone having same issues as I had.

Laravel 5 assets and lang folder in separate modules

I am trying to split my application out into seperate "modules".
I am not sure whether to leave the assets folder where it is and put everything in there for every module or how I would go about giving each module their own assets folder.
What is the best approach and what would I have to do in order to access the assets folders from each module?
Here is my directory structure so far:
Also am I right to put a Requests folder in each module?
Here is there relevant composer.json section:
"autoload": {
"classmap": [
"database",
"app/Modules"
],
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
},
Take a look at the Packages section in the official Laravel documentation. It has sections on keeping things like translation files and other assets in your package, and publishing them when including your package in your applications.

Autoloading multiple PHP projects via composer

Assume I have folder layout as
projectA
projectA\src
projectA\vendor\autoload.php
projectB
projectB\src
projectB\vendor\autoload.php
projectC
projectC\src
projectC\vendor\autoload.php
These projects need to be in the same level and they need to coexist with each others, e.g. projectA might use codes from projectB and projectC and vice vera, so they cannot be placed into the vendor folder.
The thing is: the autoload.php in each project is able to autoload their own src and vendor folder, but how to autoload for the others as well?
Assume their neighbor's project will have the folder name as the PHP namespace, is it possible to setup a autoload.php (via composer) such that in the future when I add new project folder, the autoload will magically work?
You can write in each project a composer.json with custom autoload configuration..
examples:
ProjectA:
"autoload": {
"psr-0": {
"ProjectB\\": "path/ProjectB/src/",
"ProjectC\\": "path/ProjectC/src/"
}
},
ProjectB:
"autoload": {
"psr-0": {
"ProjectA\\": "path/ProjectA/src/",
"ProjectC\\": "path/ProjectC/src/"
}
},
Composer is conceived for manage dependencies of single project.. Load more autoload.php of different projects is not a good idea..
but with this method you can create a complete autoloader for each projects

Using PHP's composer for the first time

I'm trying to get my head around using PHP's composer. In particular, I'm trying to load this project and its dependencies.
I am installing composer in the necessary folder (root of my application), so it creates a composer.phar.
I make sure I have the correct JSON file for the project in that same directory:
{
"name": "tumblr/tumblr",
"description": "Official Tumblr PHP Client",
"keywords": ["tumblr", "api", "sdk", "gif"],
"homepage": "https://github.com/tumblr/tumblr.php",
"authors": [{
"name": "John Crepezzi",
"email": "john.crepezzi#gmail.com",
"homepage": "https://github.com/seejohnrun",
"role": "developer"
}],
"license": "Apache-2.0",
"type": "library",
"require": {
"eher/oauth": "1.0.*",
"guzzle/guzzle": ">=3.1.0,<4"
},
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"psr-0": {
"Tumblr\\API": "lib"
}
}
}
I then write this in the appropriate directory with the terminal: php composer.phar install.
However, this does not load the tumblr.php. It loads other files, such as symphony, sebastian, guzzle, etc.
Am I doing this incorrectly? Does this JSON not load the tumblr.php, but its dependencies?
Composer generates a file vendor/autoload.php. If you want to use any of the things you installed with Composer in your own code, you just need to require_once 'vendor/autoload.php' and can then simply call whatever code in whatever Composer-installed library you want; the autoloader will take care of locating and including the necessary files without you having to worry about particular directories inside the vendor folder.
The autoload entry in the composer.json file is there so any library can specify particulars of how its files should be autoloaded. You do not typically have to use that yourself in your application for anything. You may use this entry to add autoloading for your own code "for free" if you wish. However, again, you do not need to add this to use any of the installed dependencies, those should all already be configured correctly for autoloading in their respective composer.json files.
If the JSON data you show is in your OWN project, you are doing it wrong. That JSON data is from the original Tumblr project, and you shouldn't copy it into your project, because as you found out, it will not really help you using the Tumblr client.
The correct way of using Composer:
Start your project by having a main directory (probably already with files).
Run composer init to easily create the initial composer.json file.
You will be asked if you want to include dependencies. You may answer yes and add tumblr/tumblr as the dependency of your project.
Alternatively, if you already have a composer.json, you can call composer require tumblr/tumblr:~0.1.
To use the library in your code, you have to include the file vendor/autoload.php and can then create all the classes according to the documentation.

Categories