Pages

Saturday, December 23, 2017

Php Generator Trait added to the Waryway Traits Library

Introduction to a Generator Trait

The trait library has grown! The newest addition is the Generator. There are lots of guides online about PHP Generators. Now, a specific 'file line reader' generator is available in the Waryway trait library. Let's take a look at what a generator is, why would it be needed, and an example of how it was implemented.

What is a Generator?

In the context of PHP, a generator is a syntax construct where a function 'yields' a value without instantiating an entire set in memory. Maybe think of it like a magic variable that yield an iteration each time you call it. 

Why would a generator be needed?

In the rare event you need to read from a line based file in PHP, I highly recommend a Generator pattern. It's a quick implementation, and less memory intensive approach to reading from a file. If you need the whole file in memory at one time though, stick with file_get_contents. Essentially, any iterable operation can be conducted with a generator. However, the only time to truly use one, is when only one line at a time is relevant, as opposed to the entire set. An example would be search a file for an entry without sorting it. Just run through the file until the item is found. The complexity is O(n),but the memory footprint is only 1 row.

Example of Implementation

I built a file line reader into a generator as a trait. This can be found in the php-traits-library on github. I like the availability of traits to build a 'base' system without intense inheritance models in PHP (abstract base class, anyone?).  For this trait, it does not cover all generator options, but specifically the line by line file reading approach. I added the typical file reading safety features, but I did not add a terminator - that might actually be a good future idea. What I did add was a callable argument, to allow a closure for parsing the line before return. Passing in a null will just return the line, unprocessed. 

Unit Testing Bonus

I've also built out an example of testing a generator (note: if you are testing something that uses a generator, phpunit has a mock approach for that).

Conclusion

I have as of yet to build out the implementation around the generator. For now - it is in place and ready to go and I've included the latest repo into the service MicroServiceEngine. 

Saturday, November 25, 2017

Waryway Website built on Aurelia hosted locally on Omega Onion2!

Announcement

I have deployed the new Waryway website FrontEnd based on Aurelia. This website is currently hosted on a dynamic ip address,with an Omega Onion as the server.

My main goal in this endeavor has been a 'hobby' website running with minimal power consumption. I had previously hosted websites on a full desktop computer (600w powersupply). This desktop running 24/7 had a marked spike in my energy bills after I got it running. I'm happy to report the omega onion is a much smaller footprint. 

I've had good uptime on a static PHP website hosted with reactphp - but I've decided to redesign the frontend/backend architecture. The new approach is an entirely Aurelia frontend with Microservices written in PHP, using reactPHP

The website itself is my initial Aurelia implementation - I have yet to link it to a microservice. Look to that in a following post.

Specifications:


  • Omega Onion2 
    • Nodejs
      • http-server
    • screen
  • Windows 10 'build' machine
    • Aurelia
    • Waryway\Frontend
    • au build for production
    • winscp for deployment

Deployment:

  1. Followed Aurelia's build instructions
  2. copied The scripts, bootstrap, and index.html files to the omega
  3. Started the http-server in screen
  4. Checked on an external network
  5. Profit!

Sunday, November 19, 2017

Setting up a Packagist PHP library using GitHub / Composer

Today I setup a new packagist entry for WaryWay's trait library. It was much easier then I expected. I have previously setup composer packages locally. This time, it wasn't much different. I build out the composer.json. Set the name. Then went ahead and tagged a semantic version. After I had all this in place and pushed up to GitHub, it was a simple matter to publish on packagist. 

Here are the exact steps:

  1. Follow the documentation on https://packagist.org/packages/submit
  2. Add the integration / services setting in github (See example1).
  3. Once setup with your token, click the 'test' button to let packagist know it is integrated.

Example 1


Monday, October 23, 2017

Bronze Sword Casting Birthday Present

For my 30th birthday my wife decided to do something special. She happens to be an excellent gift giver, and this time she had outdone herself. With my background in history, and my interest in all things nerdy... My wife had found a local Bronze Sword casting class! Now, I didn't actually know a whole lot about bronze swords - just a general idea that they existed and that they fell by the way due to Iron. That said - the class sounded really exciting! 

The class was over a period of three days. The first day involved packing a sand cast. A sand cast is one inexpensive method for holding molten bronze while it quickly cools. It's also reminiscent of making sand castles. Here you can see the impression left by the mold I chose.
After choosing the mold, the next step was to heat the bronze in a kiln, and poor it into the cast.


After the bronze had cooled, which made time go slowly because of anticipation, the sword was removed from the cast. As you can see, some of the sand stuck to the bronze, and, the bronze itself was quite rough.


On the second day, a considerable amount of time was spent sanding down the sword. It was quite a noisy endeavor! The first pass was a rough sandpaper. It made the metal gleam, but, was still quite rough. After we stepped through the levels of finer sandpaper, the sword was really starting to take shape.






The final day, we put the handle on the sword. This involved a hammered rivets. The handle we added reminded me very much of Chicago Cutlery.  


Many Hammer strikes later, and after a little bit of polishing of the handle, the sword was complete.

Bonus: After the class was over, I headed over to Hobby Lobby and picked up some leather. I used this to put together a sheath. Here is the final product. All in all, it was a great birthday present from my wife. :)


Saturday, October 21, 2017

Guide: Implementing a PhpUnit Development Environment in PhpStorm

Introduction

Unit testing is one of the most critical aspects of Continuous Integration. For some background, I like the idea of TDD, but in the end… I am a developer, I code first, ask questions later. A sample repository can be found on GitHub

Preparation

For this example, I will be starting with the following:
  • PhpTraits repository https://github.com/Waryway/PhpTraits
  • PhpStorm Project
    • Make certain you have a php interpreter referenced in your storm project
    • Also make certain you have Composer referenced by PHPStorm
  • A desire to test.allTheThings()



Getting started

Let's start from the very beginning

First, make composer do some work. We need a copy of phpunit.
Get it with this:
"require-dev":{ 
    "phpunit/phpunit":"6.4.3" 
}

As this is a 'library' style app - use an exact version. The lock file gets ignored in favor of precise dependencies in the composer.json.

Now run composer update to pull in the requested phpunit package.

Setup phpunit in the general settings, under phpunit


Adding a new test

Or rather, a test skeleton

  1. Open the file to test in the Project View
  2. Place you cursor on a method to test within the file
  3. Go To Navigate -> Test
  4. Create New test
  5. I tend to set mine up with a test prefix to keep tests more obviously separate from code.
  1. Click Ok then Open up the new file
  2. Add a line to pull in the composer autoloader
  3. Clean up the Namespace reference to the TestCase if you are using PSR-4
  4. Disclaimer: Strongly avoid namespacing unit tests. They should not be built out like a code base. They are testing units.
  5. Add an empty testHydrate method.
  6. Add a super obvious assertion.
  7. Add a phpunit.xml file
    1. I put it in the test directory.
  8. Run the test (At this point, I needed to restart phpstorm to detect that I actually had a 'test' to run.
  9. Build the tests out further.

Conclusion

Ending of the beginning

I've gone ahead and built out the 'hydrate' test against a trait. Note the use of the trait object that phpunit provides. Pretty slick, right?  I'll continue to build out this repository - core libraries need the most directed test coverage.


Even adding the 'test' cases around the hydrate method - I have found use cases I didn't consider while writing the code - and have adjusted the code to reflect the behavior I expect.

You can find this repository on GitHub! This post might need some more work, feel free to ask questions or for updated instructions.  Thanks!

Tuesday, May 2, 2017

In response to a statement about 9 to 5 developers never becoming engineers

(original post)
Ah, be careful with the stereotypes and labels. Developers that don't care will never be software engineers. This is true. Shift workers don't always fit into that category. Personally, I don't often learn much at conferences. As you've said. It is available online. Corporate environments often squelch the engineer out of developers. Concepts such as, "if it ain't broke, don't fix it" and "if we don't have a client paying for that improvement, we aren't working on it" are hard to fight. Getting creative and working the item to improve into a client's bill feels dirty for most, but "the ends justifies the means", right?

If you've ever watched any of the Gordon Ramsey cooking shows, most individuals are accused of having given up at some point. Development in a corporate environment often causes this, but doesn't highlight the issue. I often try to gauge where a developer stands by offering a suggestion and seeing if they follow it. Giving them the choice between paths will show where their interest and mettle lie. I'd once read that you don't want to hire 'b' players. You've described the 'b' player quite well. A 'b' is strong enough where they don't feel they need to grow, but not motivated enough to push themselves past that point.

A true leader, not just an engineer, is able to take this complacent individual and push them. Most middle management is management, not leadership. In development environments with strong leadership - the apathy levels are low. In development environments with strong middle management, the apathy levels are much higher. As for anyone seeking their next position, keep an eye out for weak leadership. It will either be an opportunity, or a demise.