Moc10 Library - A Lightweight PHP Library.

August 31, 2009 in Development & Programming

Tags:

I finally finished and released my PHP library - Moc10 Library. Special thanks to Ralph Schindler for all the help along the way.

So, the library is the result of a collection of code I've written over the past two years or so, which is the most likely way that a library gets started. I figured if I'm going to keep reusing this code, I might as well organize it in some fashion.

The Moc10 Library is comprised of a total of 9 components: Autoloader, DB Connection, File, Form, Image, PDF, Record, Session and String. Some of the components work independently of each other, while some of them depend (or require) of one or more the others to fully function.

A Brief Overview:
  • The Autoloader Component - Allows you to load the classes of the library, as well as any classes from your application, into the autoload stack, eliminating the need for the usual require/include calls.
  • The DB Connection Component - Allows basic access to a MySQL database using MySQLi. With it, you'll have access to various methods and properties to execute queries and fetch their results. The database credentials can either be set once with in the class, or passed on the fly as needed.
  • The File Component - Allows for the basic manipulation of files and file uploads. It includes methods to read, write, copy, move, mail, output and delete files. It also contains useful properties of the file, such as the directory path, mime type, extension, etc.
  • The Form Component - Allows you to create, render and validate HTML forms and form elements. You can render the form using a template via $form->render(), or via a basic print($form) output call.
  • The Image Component - Allows for advanced control and manipulation of images and image uploads (GIF, JPG and PNG formats only.) It extends the File component and inherits its methods and properties, as well adds a few useful methods to write text, resize, scale, crop and convert.
  • The PDF Component - Allows for basic text, graphic and image manipulation within the PDF format, as well as the importing and manipulation of existing PDFs and their pages. It extends Moc10_File and utilizes Moc10_Image.
  • The Record Component - Acts as a pseudo-active record pattern, but uses a child class to actually define the table. The child class then inherits all of the methods and properties of the parent Record class and allows for basic CRUD database manipulation with methods such as findById, findBy, findAll, save and delete. It also includes more advanced methods such as search, join and distinct
  • The Session Component - Allows for streamlined access to and manipulation of the $_SESSION global variable.
  • The String Component - Allows for easy and useful manipulation of strings.

Now, most of the code for the library has been written over the past 2 years and has been fairly well tested in a handful of applications I've built for clients as well as myself. However, the last piece of the puzzle that I decided to "throw in" on a whim without releasing exactly what I was getting into was the PDF component. Over this past summer, I researched the Adobe PDF Development and Reference manuals and began to dive into the world of PDF and the PostScript language. Oh what fun! (Please note the sarcasm.) Ah, PostScript - a lovely little "language" that was created in 1982 and has only seen a total three "versions" (or levels) since its launch 27 years ago. To me, the language seems to only be a step or two above assembly language in the readability department. And, I find that the ultimate irony is that, in the mid-90's, Adobe "opened up" the PDF standard for anyone to create software to write and read PDF files, however the "language" behind it is fairly difficult to get your head around it. Thanks Adobe!

So, what got me going down the dark, ominous road into the world of PDF, was that I had some initial quick success. Once I deciphered some of it, I was able to get a couple of the "basics" to happen - text, vector graphics, color and stroke control. Great. I'm on my way right? Wrong. That was only the tip of the proverbial iceberg. I created a list of features I wanted to build into the PDF component, based loosely on what others had accomplished out there. By the way, I'd like to take this opportunity to give a shout out to Oliver and his FPDF class, as I've used it before with great success and it was an inspiration to me as I was tackling the PDF component of my library (he even answered a question of mine regarding images.)

Anyway, each feature on the list proved to be a even more of an undertaking than the last. Internal link and external URLS - ok, got that down after some playing around. Save, output and email the PDF - not too bad. On to images - holy crap! I learned things about image formats I never dreamed I would care to know. That was a tough piece of the equation. Fortunately, the Image component of the library came in handy. Then came the pages. Adding a page - not too bad. Selecting which page to append content, a little tricky. Copying and ordering the pages - uh oh. Importing full PDFs and their pages, or just certain pages of an existing PDF - now we're in trouble. What was I thinking?

I took a step back and looked at the big picture of everything I had read and learned about the PDF format up till now. One common thing was that it basically treats everything as an object. And objects can reference other objects. So I changed the way I looked at the PDF class (which was growing exponentially into a ridiculous God object) and I broke it out into multiple classes - each that represented a real PDF object that I felt was important to the PDF structure. So the root, parent and info PDF objects each got their own class. The concept of the PDF page got it's own class. The idea of a real world PDF object (content, content stream, etc.) got its own class. And, the special case of importing a PDF got its own class. Therefore, the main PDF class (Moc10_Pdf) became a "manager" object to all the things that happen "behind the scenes" in the land of PDF. At the same, I tried not to loose focus on delivering a easy-to-use API for the developer who just doesn't want to know anything about that and just wants to create and output some PDFs.

So, it was a pretty intense road to traverse, but overall, I think the end result is a good place to start. I would like to add a couple of features that I know will be the first questions asked about the PDF component (i.e. compression and embedding fonts.) But, I'll leave that for the next version.

As for the library as a whole, I'm putting it out there with the hopes that I can get some feedback, input and discussion from others out there in an effort, as with all things open-source, to make it better through the community. I hope you all like the first version of it. Let me know your thoughts. Thanks!

Comments
  (Refresh)
 
There are currently no comments on this page.