Reputation: 35963
I have 3 projects that are very similar: 95% of the code. Now when I have to make an edit I have to copy that three times and It's very bad!
So I need to have only one project and manage the small differences with something like environments. But I dont't really know the good way.
For example I have a controller with this function
public function save()
{
$this->validate($this->request, [
'firstname' => 'string',
'lastname' => 'string',
'gender' => 'string',
'phone' => 'bail|size:10|required',
'email' => 'email',
'birthdate' => 'bail|date',
'referral_code' => 'bail|string',
'city' => 'bail|string',
'call_state' => 'bail|string',
'call_date' => 'bail|date',
'campaign' => 'bail|string',
'note' => 'bail|string',
'overwritten_by_lead' => 'bail|integer',
'change_state_counter' => 'bail|integer'
]);
$data = $this->request->all();
$customer = $this->repository->create( $data );
return $this->response->item($customer, $this->transformer)->setStatusCode(201);
}
and the difference with another project is:
public function save()
{
$this->validate($this->request, [
'firstname' => 'string',
'lastname' => 'string',
'gender' => 'string',
'phone' => 'bail|size:10|required',
'email' => 'email',
'birthdate' => 'bail|date',
'referral_code' => 'bail|string',
'city' => 'bail|string'
]);
$data = $this->request->all();
$customer = $this->repository->create( $data );
return $this->response->item($customer, $this->transformer)->setStatusCode(201);
}
Another difference inside entities are for example:
protected $fillable = [
'firstname',
'lastname',
'phone',
'email',
'birthdate',
'gender',
'province',
'zip_code',
'address',
'city',
'campaign',
'overwritten_by_lead',
'change_state_counter',
'call_state',
'call_date',
'credit',
'referral_code'
];
And this is another
protected $fillable = [
'firstname',
'lastname',
'phone',
'email',
'birthdate',
'gender',
'province',
'zip_code',
'address',
'city'
];
Inside thhese three projects there are very little differences but I need to manage these.
I don't want to manage with many If because environment can be more than 3!
What is the best way to overwrite or to manage these difference into different environments with different files?
For example I have for project 1 inside a folder: CustomerController and CustomerEntity The same thing for the other project, inside every folder of an environment I put the files that are different, but how can I overwrite these files?
Thanks
Upvotes: 2
Views: 403
Reputation: 5963
Why not just abstract the shared code/attributes into a custom package and work from there? You can just extend the 'base' classes from your package in your current project and only pass additional data into those methods when required or overload them completely if you defer too much.
That will take away the need to check for which project you are using.
For example:
- /vendor/package/path
- Models
- BaseModel (extends eloquent)
- ModelA (extends BaseModel)
- ModelB (extends BaseModel)
- Controllers
- SomeController (extends base controller)
- ...
And in your app you can just create
- /app/path
- Http
- Controllers
- SiteController (extends package controller)
- ..
- Models
- ModelA (extends package ModelA)
- ...
You can add your shared code in the package (for example all shared fields) and leave them alone when no changes are needed. If you need to do custom foo for, let's say, SiteA, just overload the fillable or validation rules (or create a method in your base classes which can merge them to your likings).
This way you can utilize all shared functionality and overload custom attributes or methods where needed.
This doesn't have to be a package btw, you could just as wel define a Library or Shared namespace and use classmap magic in your composer.json
"autoload": {
"classmap": [
"database",
"app/Library" <--
],
But in your case I would suggest creating a package, 1 codebase to maintain.
Upvotes: 0
Reputation: 1165
I would suggest one approach as below.
Step 1 : Create file say config\myenv.php
and add contents as below
<?php
/**
* Create default Environment
*/
return [
'current_env' => 'envA'
];
Step 2 : As per your requirement or conditions change the environment dynamically as below. (You may change depending on login or wherever you want)
//Consider you have 3 environment like envA,envB,envC. change according to them
Config::set('myenv.current_env', 'envB');
Step 3 : Now say you have a save()
which is common in 3 diff env with slight difference. You can do as below. (Imp:Consider after changing env)
public function save(){
// Config::get('myenv.current_env') will return current loaded environment
if(Config::get('myenv.current_env') === 'envA'){
//You code for EnvA
}
if(Config::get('myenv.current_env') === 'envB'){
//You code for EnvB
}
if(Config::get('myenv.current_env') === 'envC'){
//You code for EnvC
}
}
Step 3 : For you models
I'll suggest to create separate model
for each environment. Like as below,
Consider right now you have Repository
Model. (app\Repository)
If you edit in same model later on it will be complex to maintain. You can can create separate models for each env like below.
app\Models\enva\Repository -> For Env A
app\Models\envb\Repository -> For Env B
app\Models\envc\Repository -> For Env C
Now you can use them in controller as below
if(Config::get('myenv.current_env') === 'envA'){
// App\Models\enva\Repository::your_methods()
}
if(Config::get('myenv.current_env') === 'envB'){
// App\Models\envB\Repository::your_methods()
}
Likewise you can differentiate the environments.
Important is how you are going to detect environment
Hope it helps you!
Upvotes: 2