Automatic REST API for SlimPHP 4

Today, about 4 years after the initial commit, the promise of “upload a single PHP file to add a REST API to your database” is still very much alive. It is now possible to use PHP-CRUD-API (2k stars) as a library executed on an endpoint in the SlimPHP 4 framework (10k stars). This is possible as they both adhere to the PHP-FIG’s (PHP Framework Interop Group) HTTP standard PSR-7. Install SlimPHP 4 You need to run the following commands on your Linux system: ...

August 16, 2019 · Maurits van der Schee

Script to undelete all files in Git

I have written a Bash script to quickly undelete all files that are deleted in a Git repository (on any previous commit). The script only recovers the last known state of files of which the filename is not currently in use. It is optimized so that it executes quickly when large numbers of files are deleted. Usage Copy the script into the repository that you want to undelete files in and run: ...

August 13, 2019 · Maurits van der Schee

API authorization strategy: use the DB

When building an API you may find the need to implement authorization in a generic way. Using the authorization implementation of your (relational) database is a well-documented, simple and proven strategy. The user that is used for the database connection should in this scenario depend on the authenticated user of the API (and it’s authorization). This post will explain how to apply this strategy. But before we start let’s take a step back and think about what it is that you may be authorizing. ...

July 29, 2019 · Maurits van der Schee

Database multi-tenancy strategies

When you are building a SaaS software product you need to chose a way to store the data of your customers. You can store everything in one database or you can create multiple databases. There are roughly four main approaches. This article discusses the up- and downsides of these approaches. Multi-tenancy: 4 approaches First let’s try to summarize the four identified approaches: All customers in one database (fully normalized) Every table has a CustomerID field (de-normalized) A database for every customer and one shared database Every customer has it’s own database Now let’s try to find some up- and downsides for each of them. ...

July 28, 2019 · Maurits van der Schee

Handling GeoJSON tiles in Leaflet

Leaflet is world’s most popular open-source JavaScript “Tiled web map” library offering Google maps like functionality on your own (or public) data. In the past weeks I have implemented a GeoJSON vector tile plugin for Leaflet. In this post I will explain when to use raster tiles, Mapbox Vector Tiles, GeoJSON tiles or a GeoJSON bounding box. Why tiles? When somebody has a free pan and zoom over a map, no two maps are the same, as you have a lot possible visible maps and sizes and zoom levels. For a single user this would be fine, but as soon as you have multiple users then you want to use some form of caching to reduce the load on the server. You could fix the map size and pan and zoom in fixed steps, but that does not feel good. This is why tiling is introduced. It allows you to have the caching advantages of fixed steps, while allowing the user custom map sizes and seamless pan and zoom (Google Maps has fixed zoom steps, Google Earth has seamless zoom). ...

July 20, 2019 · Maurits van der Schee

Lossless compression of PHP files

How small can a PHP file get? I was wondering this, while building PHP-CRUD-API, a full-featured API in a single PHP file. PHP has a really nice feature called ‘halt_compiler’, which allows you to have gzip contents in your PHP file. In the code below I’m applying that technique to PHP code. This allows you to reduce the size of a PHP file without losing any of it’s functionality. Why would you do this? The file gets smaller, but executing it will be slower as it is need to be uncompressed before it can be executed. I also think the opcode cache may have more trouble optimizing your code when you uncompress it on-the-fly. Despite these downsides, some people use similar techniques for obfuscating the source code they publish. So I guess you could use this script to prevent people from easily reading your source code. I have not found any other (good) use case (yet). ...

July 19, 2019 · Maurits van der Schee

Open source software pyramid

For us (professional software developers) there are open source libraries and tools that cover most of what we are doing. Even in commercial projects we can often leverage these as their licenses are often permissive towards commercial use. Which raises the question: When do you write your own code? On a GDS blog I ran into the “open source pyramid” as advocated by JP Rangaswami: For common problems use Open Source. For rare problems use Buy. For unique problems use Build. This is a pattern I have often encountered in commercial software companies. I have seen exceptions to this rule when the software is very important to the company (for instance part of the strategy or “core business” of the company). ...

June 11, 2019 · Maurits van der Schee

PHP-CRUD-API gets GeoJSON support

PHP-CRUD-API is an automatic API script: Upload a single PHP file and get an instant REST API to your PostgreSQL, MySQL/MariaDB or SQL Server database. Currently support for GeoJSON FeatureCollection views on tables has been added. The REST API already supported GIS (Geographic Information System) functionality through WKT (Well Known Text) formatted output for geometry columns in the database. The GeoJSON support allows to quickly add a table as a map layer to for instance QGIS. ...

June 4, 2019 · Maurits van der Schee

Celebrating 3 years TQdev

Today I am celebrating the 3 years that the TQdev.com blog exists. In this period I have written 125 blog posts on various software development related topics. Best visited post was the “The “Boring Software” manifesto” with more than 33 thousand visitors. Below you find the visitors of the blog per month. Visitors graph The graph below is a (copy of) a server side generated SVG document from the backend of this blog. ...

May 9, 2019 · Maurits van der Schee

PHP templating engine from scratch

In a previous post I have introduced PHP templating in 165 lines of code with no dependencies. I have added several features since it’s initial release and the line count has roughly doubled. This post gives you an overview of the added functionality and explains how to use it. An ‘if’ now supports ’else’ and ’elseif’ Conditionals are an essential construct in a template. When you implement them without ’else’ or ’elseif’ you get either a lot of repeated conditions and unnecessary nesting. ...

May 1, 2019 · Maurits van der Schee