Category: PHP (page 1 of 2)

Aurelia orm restful apis

Today I released a new version of aurelia orm. You can find it here: https://github.com/SpoonX/aurelia-orm

The reason I built it, is because I was missing a nice, structured way of working with my Rest API resources. Sure, There are libraries out there like breeze. But in my opinion, they are huge and needlessly complex; which is exactly what I want to avoid.

Aurelia-orm adds some cool features like entity definitions, repositories, associations and validation. This makes it easier to organise, too, seeing how most logic can now be placed in repositories.

There’s plenty of documentation, so have fun!

PHPStorm / Webstorm plugin acejump

Fresh IDE, happy me

Today is a good day. I got to configure my IDE on a new machine, which is always a joy. I started setting up the basics such as code style, libraries, line numbers, eof newline etc and moved on to the plugins. To my surprise, very few people I work with seemed to be familiar with an amazing plugin called AceJump. This triggered me to write about it and try to better your IDEs one by one.

What is AceJump?

AceJump is a plugin by johnlindquist that allows you to jump your cursor through a file, by simple find and magic. The plugin itself has the following description:

General usage instructions
Hit “ctrl+;”, type a char, then select a char to jump to. When selecting the char “shift” will select the block between the cursor and char. You can toggle “target mode” by tapping “ctrl+;” again, which will select the word you jump to. You can remap the AceJump keyboard shortcut in File->Settings->KeyMap and search for “Ace Jump”. Right-click on it, then select a different shortcut.

The simplicity of the plugin, and the power it gives you makes it an important tool to have in your arsenal. More info about the plugin can be found in this youtube video by John: Ace Jump.

Where can I get it?

You can find the plugin by clicking here or by going to File->Settings->Plugins->Browse Repositories and searching AceJump.

Documentation for everything

Hello fellow geeks!

Today I’d like to share a nice piece of software that has boosted my productivity by 200%. It’s a tool that allows you to download documentation from a lot of libraries, frameworks, programming / scripting languages, stack overflow, packagist and more. It’s called Dash (for mac) and Zeal docs for Windows / linux. For this post I will be talking about Dash specifically, but most of it also applies to zealdocs.

What it does

Like they say themselves:

Dash gives your Mac instant offline access to 150+ API documentation sets.

It integrates with the system (shortkey) and a lot of editors / other systems. As an example, in your editor, you can select some text (for instance, Angular’s $http) and push the shortkeys for Dash (defaults to cmd+shift+d), which will bring up the documentation on $http for angular.js. Are you writing php, and are you unsure on what strstr or stristr do? Select that, press the shortkeys and boom, documentation.

Like I said, it has documentation for a lot of things. Node.js, PHP, javascript, ruby, Zend Framework 2, Laravel, and much, much, much (I can keep going like this) much more.

It also supports Packagist and automatically builds documentation for all packages you wish to use, if there aren’t any. And it supports stackoverflow by downloading answered questions which you can search.

Download

It’s offline, fast, has everything in one place and doesn’t force you to wait for the browser. It’s just cool. Check it out:

Mac: Dash

Windows/Linux: Zeal docs

Survival guide: Many websocket connections

When working with sails.js, or indeed any software that accepts websocket connections, you have to be careful you don’t start rejecting requests. It’s all about finding the perfect balance between CPU, memory and file descriptors. Unfortunately, there is no such thing as a “one size fits all” solution in this case, because it all depends on your application.

I’ll try to highlight the importance of certain aspects by providing examples with them. I’ll explain why, when and how they’re important, providing possible solutions with them as well. For this article I’ll assume you’re using sails.js, but the suggestions given in this article apply to any application. Let’s get to it.

Scaling

Scalability is something to always keep in mind. You can read more about scaling sails.js here.

Horizontal scaling

You can read about this more by reading this article. To me, this is one of the most important things to keep in mind while setting up your server architecture.

The most obvious way to make scaling possible, to me, is by using redis as the session store and pubsub adapter. That way, you’re not memory-bound and you can simply add instances and VMs, where you apply load balancing.

System scaling

This is a pretty simple thing to set up. Using PM2 you’ll be able to scale your application over multiple cores on a single machine. Sometimes, an extra core is cheaper that an extra VM. This all depends on how much memory you’ll need to add, and how much CPU power is actually being used.

Resources

It’s crucial to know exactly how much memory you need, and to keep an eye on how much you’re using. If you have a memory leak, or a sudden increase in activity your application might crash (which can obviously be recovered using forever.js) your application might crash, and we don’t want that to happen. In general, you should be careful with what you store in memory. Just as with memory, it’s also equally as important to keep an eye on your CPU power.

File descriptors

File descriptors, as taken from wikipedia:

In computer programming, a file descriptor (FD) is an abstract indicator for accessing a file.

Everything, even network connections use FDs. By default, the limits set for them are quite low making it quite probable you’ll find yourself looking at errors that only show up in your /var/log/messages log, and don’t make a whole lot of sense. Capping this limit, will cause connections to drop, communications to fail and basically ensures chaos.

Viewing the limits

There are two types of FD limits: system limits and user limits. The system limits can be viewed by running:

the user limits can be viewed by signing in as the user (su username) and running for the hard limit:

And for the soft limit:

Changing the limits

To change the system limits, run:

Open up vi /etc/sysctl.conf and add the following at the end of the file: fs.file-max = 200000.

Finally, run sysctl -p.

For the user, open the file vi /etc/security/limits.conf and add the following lines (replacing username with the username you’re using):

Save the file, and done.

The right limits

Figuring out what the right number is for your server depends on your application. If you notice you’re capping on file descriptors, with plenty of bandwidth, memory and CPU left it’s probably safe to increase the number by quite a bit.

Zend Framework 2 modules

About a week ago SpiffyJr created a post on his blog outlining his modules and their state. I quite liked the initiative, and felt like following up on this (what I hope to become) trend.

The reason I feel this is a good idea, is because there seems to be a lot of duplicate module creation. Zend Framework 2 developers don’t always realise something already exists, so decide to build something themselves. This is a waste of time, efforts and talent, which are all things I hate wasting. For this reason, I’ve compiled my own list of modules. So following is a description of my modules.

Modules

Most of my modules reside under the SpoonX organisation. The modules that make it to the organisation are stable, or at least complete enough. Currently, the only module that’s stable but not under the spoonx namespace, is the AssetManager module.


AssetManager

Summary

Description

As perfectly described in the readme:

This module provides functionality to load assets and static files from your module directories through simple configuration. This allows you to avoid having to copy your files over to the public/ directory, and makes usage of assets very similar to what already is possible with view scripts, which can be overridden by other modules. In a nutshell, this module allows you to package assets with your module working out of the box.

On top of this, the module also allows you to apply filters, apply caching, create collections, create custom resolvers, create custom filters and create custom cachers.


SxBootstrap

Summary

Description

This module is a wrapper for twitter bootstrap. It comes with some neat features allowing you to do some cool things.

Features (as taken from the README.md):

  • Simple awesome forms (group action controls, create horizontal forms etc)
  • Fast
  • Customisable (variables, components, plugins. No more downloading zip files!)
  • Works with the AssetManager
  • Plenty of ViewHelpers
  • Extendable (run your own less files from bootstrap context to share mixins and variables)

SxBootstrapWizard

Summary

Description

There’s not much to this module. It’s a plugin for SxBootstrap that allows you to easily create stunning wizards.


SxCore

Summary

Description

This is (currently) a very small library. It has two components.

SxCore\Html\HtmlElement

The HtmlElement is pretty neat, it allows you to easily create markup using php. This is especially useful when creating viewhelpers; which is the reason it’s being used by SxBootstrap so intensively.

I started writing this component because of the annoying way Zend\Form\Element were written, and the restrictions they had. I was constantly messing around with strings (eww) and found myself unable to add ng- attributes due to restrictions (which, if you’ve ever used angular, is extremely annoying).

You can find some examples here.

SxCore\Html\ExcerptExtractor

This neat little component allows you to extract excerpts from bundles of markup without breaking the tags. This is really useful when it comes to, I don’t know, writing a blog (Yes, I’m hinting at SxBlog).


SxMail

Summary

Description

This module makes it incredibly easy to set up mailings. It does all of the boring work for you, and supplies useful functionality such as using ViewModels as templates and wrapping those templates in a layout. It also allows you to set up multiple configurations for multiple mailings, which will be merged with the default configuration (marked by the “default” key).

Features (as taken from the README.md):

  • Easily configurable. Both messages and transport.
  • Use ViewModels
  • Layout support
  • Mimepart detection
  • Easy header configuration
  • Access to Zend* classes
  • Generates alternative (text) body
  • 100% code coverage
  • Scratches your back after a long day of work
  • Makes a good drinking buddy.

SxBlog

Summary

Description

This module is an attempt at creating a skeleton blog for minimalist bloggers (such as myself). It already includes a handful of features, but isn’t complete yet.

Here’s a screenshot of it in use:


SxSkeletonModule

Summary

Description

This is not really a module. It’s basically a skeleton module (yes, I know, the repository name sort of gave that away) that allows you to easily start a new Zend Framework 2 module. It includes a license, composer.json, a module structure and basic tests. It’s easily used by running the setup script that’s bundled with it.

The following is the easiest way to get this up and running: git clone git@github.com:SpoonX/SxSkeletonModule.git; cd SxSkeletonModule; ./setup.sh Username ModuleName; cd ..;

Note: Make sure you change the Username and ModuleName to yours. It’s used for the composer.json (and more files).

Conclusion

Writing this post took me quite a while but it was worth it. I hope it’ll help a developer or two. Building modules is awesome; but nothing’s as satisfying as watching people use, and enjoy using them.

There are obviously a lot more modules out there. A while ago Hounddog created a Zend Framework 2 modules application for which I helped out with the styling. It’s a really neat resource if you’re looking for modules, and I strongly suggest you submit your modules, too!

Happy coding!

zend framework 2 blog module

Dear WordPress,

After many years of fiddling with you, it seems I have finally reached my breaking point. It started off as exciting, hot, heavy and fun, like most relations do, but it has become an annoyance to deal with you. The resources you ask of me, the many confusing options you present me with… It’s just too much. I can’t deal with it anymore, and I want to be able to be myself again! I’ve had it with adapting to your needs just so I won’t hurt your feelings.

So it seems that the time has come for me to build my own blog based on Zend Framework 2. I’ve already started with the module and it has been a joy working on it so far. For now, it includes the following features:

  • An overview of posts
  • An overview of categories
  • An overview of posts by category
  • Pagination
  • Create new post
  • Delete post
  • Edit post
  • View post
  • Create new category
  • Delete category
  • Edit category
  • Customisable messages (for deletion, creation, updates etc)
  • Theme support (really just by supplying view files. Extended support will follow)

The module can be found here: https://github.com/SpoonX/SxBlog

There’s more to come, but for now this is more than enough for me to leave you. It’s just younger, more organised and it listens to me. And in the end, it was ‘The Loop‘ that drove me away from you…

It’s not me, it’s you. But you will forever hold a place in my memory. I wish you the best of luck. in the years to come.

No longer yours,

Roberto Wesley Overdijk.

PHP sandbox online editing

I’m guessing you got to this page because you like PHP, and you like testing without creating entire directory structures or using php -r ”;.

Well, me too. That’s why I went out trying to find a tool that could help me with exactly that. One problem! There were none. And the ones that I did find also did not work the way I wanted them to. They were either online, too bloated, or required ftp access.

So, I built one. Yes, yes I did. You can find the code here: https://github.com/RWOverdijk/SxPhandbox

Installation should be pretty straight forward, as explained in the readme.

Runtime errors in the editor

Have fun!

Redirecting from a controller in ZF2

Hey all. Today’s post is a response to a question asked today on IRC.

The question:

In zf1 within a controller I could do something like $this->getResponse()->setRedirect(‘/foo/bar’, 201) ~ How do I do that using zf2?

The answer:

Controllers have plugins, not much has changed there. Except for the fact that it’s now even easier to get a redirect going. Check this out:

$this->redirect()->toRoute('routeName');

It also has a toUrl method, which behaves the same way:

$this->redirect()->toUrl('http://google.nl/');

Counter question:

Can you send a statusCode with that though?

Counter answer:

Yes, you can. As seen here, the redirect returns a Response. A response has a setStatus code, thus allowing us to do this:

$this->redirect()->toRoute('routeName')->setStatusCode(201);

Happy coding 🙂

 

Asset manager module for zend framework 2

Not very long ago I published the first stable version of the AssetManager module for Zend Framework 2. The purpose of the module is to allow you to package assets with your module, in stead of having to mess around with symbolic links, aliasmatch or mod_rewrite. On top of that, this module also gives you some other functionality such as caching, grouping assets and applying filters to your assets. You can add custom filters, custom cachers and custom asset resolvers.

https://github.com/RWOverdijk/AssetManager

unit testing unlink

Writing unit tests is very important. However, sometimes it requires you to be creative as it doesn’t always allow you to do what you want to. One of these things is testing a failed unlink.

I was testing a file system caching adapter using phpunit, and this became quite the challenge. Testing the has, set, and get were easy peasy, but removing the cache was rather difficult. I wanted to make sure that I didn’t have permissions to remove the files in question. I worked my way around it by simply trying to remove /dev/null. This is not allowed and results into the error I wanted.

I hope this is useful for you.

Cheers.

©SpoonX 2017