throwing laravel into your legacy app™
TRANSCRIPT
Throwing Laravel into your Legacy™ Application
Joe Ferguson
Who Am I?
Joe Ferguson
PHP Developer
PHP Architect @ Ministry Brands
Twitter: @JoePFerguson
Organizer of @MemphisPHP
@NomadPHP Lightning Talks
Drone Racing Pilot
For Further Reading
leanpub.com/mlaphplaravelupandrunning.com
First: A Warning
Legacy Applications
“There are no solutions, only trade offs”
- Joe Ferguson
- Paul M. Jones
- Thomas Sowell
What is a "Legacy" application
Legacy is often used for an older production application that was written before, or without regard for modern technologies or practices.
If you ask Wikipedia…
"... no-longer supported …"
"... maintained by an administrator that did not develop the code …"
"... supporting older file formats ..."
https://en.wikipedia.org/wiki/Legacy_code
Modern Interpretation
"... source code inherited from someone else …"
"... source code inherited from an older version of the software ..."
Some people say…
"Any code in production is not legacy”
"Any code written X years ago is legacy"
Legacy is all these things
As we talk about Legacy code here, we're really talking about all of these definitions
This process is not Legacy Specific…
Initial Project Overview
Is there a framework?
Is there a coding standard?
Is there any autoloading?
How are dependencies behind handled?
Is there an ORM or how is the database being utilized?
Is there a development environment?
Is there a framework?
Is there a coding standard?
There is a PSR for that…
How are dependencies behind handled?
Is there an ORM or how is the database being utilized?
Tools to help
PHP Coding Standards Fixerhttp://cs.sensiolabs.org/
PHP Coding Standards Fixerphp-cs-fixer fix app --dry-run
PHP Mess Detectorhttps://phpmd.org
PHP Mess Detectorhttps://phpmd.org
$ phpmd app html cleancode --reportfile report.html
Warning about Automating Code Changes
Triage
Address any critical things found in Contemplation phase
PHP version issues
Fixing critical vulnerabilities
“We should just rewrite the app”
"This would be so much easier in framework _____________”
Why not rewrite?
Progress halts entirely
Business logic is rarely reused
Most of the time is getting back to current functionality
Why you should refactor
Progress continues
Business logic reused
Modernizing functionality instead of recreating
Make Decisions
Build Proof of Concept(s)
Ensure stakeholder buy in
Avoid the allure of green fields**
** in some cases a rewrite is warranted
Refactoring Approach
Existing Tests
This is great!
Review the tests to get an idea of coverage
Passing tests become your control for any upcoming changes
Confidence++
No Existing Tests
Not the end of the world!
Inspect the code base, was it written to be easily testable?
Could you easily mock dependencies?
Untestable Code
Not all code is testable
If you can’t easily unit test the code base, consider alternatives
Functional testing and Acceptance testing are valid options
Acceptance Testing
NOT a replacement for Unit Testing
Test large parts of your application at a time
Can be harder to pinpoint error location
Gives large code coverage in short amount of time
Digging into the Code
Code Consolidation
Digging into the Code
Code Consolidation
Replace globals with DI
Digging into the Code
Code Consolidation
Replace globals with Dependency Injection
Write tests
Digging into the Code
Code Consolidation
Replace globals with Dependency Injection
Write tests
Extract SQL (to ORM or other)
Digging into the Code
Code Consolidation
Replace globals with Dependency Injection
Write tests
Extract SQL (to ORM or other)
Extract Business Logic (To domain logic)
Digging into the Code
Code Consolidation
Replace globals with Dependency Injection
Write tests
Extract SQL (to ORM or other)
Extract Business Logic (To domain logic)
Extract Presentation Logic to Views (Twig, Blade, etc)
Digging into the Code
Code Consolidation
Replace globals with Dependency Injection
Write tests
Extract SQL (to ORM or other)
Extract Business Logic (To domain logic)
Extract Presentation Logic to Views (Twig, Blade, etc)
Extract Action Logic (To Controllers)
Digging into the Code
Code Consolidation
Replace globals with Dependency Injection
Write tests
Extract SQL (to ORM or other)
Extract Business Logic (To domain logic)
Extract Presentation Logic to Views (Twig, Blade, etc)
Extract Action Logic (To Controllers)
Write (more) tests
Continuous Integration
Where does Laravel fit in?
You can easily** use Laravel components in your Legacy App
** Relatively speaking, and not all components
Laravel Component Goals
Implement Configuration
Implement Logging
Implement Routing
Implement Database
Implement Container
Configuration
https://github.com/mattstauffer/Torch/tree/master/components/config
Configuration
https://github.com/mattstauffer/Torch/tree/master/components/config
Configuration
https://github.com/mattstauffer/Torch/tree/master/components/config
Configuration
https://github.com/mattstauffer/Torch/tree/master/components/config
Configuration
https://github.com/mattstauffer/Torch/tree/master/components/config
What about secrets?
composer require vlucas/phpdotenv
composer require vlucas/phpdotenv
.env
config/app.php
Access Config the same way
Now our config is outside VCS
Do NOT version control your .env file!
Logging
https://github.com/mattstauffer/Torch/tree/master/components/log
Logging
https://github.com/mattstauffer/Torch/tree/master/components/log
Logging
Logging
You’ll likely want to configure your logger into a base class
that is extended
Routing
https://github.com/mattstauffer/Torch/tree/master/components/routing
Routing
https://github.com/mattstauffer/Torch/tree/master/components/routing
Routing
https://github.com/mattstauffer/Torch/tree/master/components/routing
Routing
https://github.com/mattstauffer/Torch/tree/master/components/routing
Routing
Routing
Database
https://github.com/mattstauffer/Torch/tree/master/components/database
Database
https://github.com/mattstauffer/Torch/tree/master/components/database
Database
https://github.com/mattstauffer/Torch/tree/master/components/database
Database
https://github.com/mattstauffer/Torch/tree/master/components/database
Container
https://github.com/mattstauffer/Torch/tree/master/components/container
Container
https://github.com/mattstauffer/Torch/tree/master/components/container
Container
https://github.com/mattstauffer/Torch/tree/master/components/container
Container
https://github.com/mattstauffer/Torch/tree/master/components/container
Container
https://github.com/mattstauffer/Torch/tree/master/components/container
Container
https://github.com/mattstauffer/Torch/tree/master/components/container
Containers are very powerful. Make sure you’re not adding
spaghetti code just to shoehorn into your App
Recap
Follow a refactoring process
Leverage Laravel components as you refactor
Make sure you write tests along the way!
Joe FergusonTwitter: @JoePFergusonEmail: [email protected]: joepferguson
Contact Info:
https://joind.in/talk/d306e
Feedback: