PHP Beyond the Web Shell Scripts, Desktop Software, System Daemons and More
Total Page:16
File Type:pdf, Size:1020Kb
PHP Beyond the web Shell scripts, desktop software, system daemons and more Rob Aley This book is for sale at http://leanpub.com/php This version was published on 2013-11-25 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2012 - 2013 Rob Aley Tweet This Book! Please help Rob Aley by spreading the word about this book on Twitter! The suggested hashtag for this book is #phpbeyondtheweb. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#phpbeyondtheweb Contents Welcome ............................................ i About the author ...................................... i Acknowledgements ..................................... ii 1 Introduction ........................................ 1 1.1 “Use PHP? We’re not building a website, you know!”. ............... 1 1.2 Are you new to PHP? ................................. 2 1.3 Reader prerequisites. Or, what this book isn’t .................... 3 1.4 An important note for Windows and Mac users ................... 3 1.5 About the sample code ................................ 4 1.6 External resources ................................... 4 1.7 Book formats/versions available, and access to updates ............... 5 1.8 English. The Real English. .............................. 5 2 Getting away from the Web - the basics ......................... 6 2.1 PHP without a web server .............................. 6 2.2 PHP versions - what’s yours? ............................. 7 2.3 A few good reasons NOT to do it in PHP ...................... 8 2.4 Thinking about security ................................ 9 3 Understanding the CLI SAPI, and why you need to .................. 11 3.1 What’s different about the CLI SAPI? ........................ 11 3.2 CLI SAPI installation ................................. 12 3.3 PHP command line options .............................. 12 3.4 Command line arguments for your script ...................... 17 3.5 The many ways to call PHP scripts ......................... 18 3.6 “Click to Run” your PHP ............................... 22 3.7 Quitting your script .................................. 26 4 Development tools ..................................... 27 4.1 PHP REPLs ...................................... 27 4.2 Build systems ..................................... 32 4.3 Continuous Integration ................................ 33 4.4 Debuggers ....................................... 37 4.5 Testing and Unit Testing ............................... 40 4.6 Static code analysis .................................. 43 4.7 Virtual development & testing environments .................... 48 4.8 Source/version control systems & code repositories . 50 CONTENTS 4.9 IDEs and editors .................................... 52 4.10 Documentation generators .............................. 52 4.11 Profilers ........................................ 54 4.12 Other tools ....................................... 54 5 User facing software .................................... 58 5.1 Command line interface basics ............................ 59 5.2 Advanced command line input ............................ 63 5.3 Using STDIN, STOUT & STDERR .......................... 66 5.4 Partial GUI elements - dialogs ............................ 68 5.5 Dialogs invoked from the shell ............................ 68 5.6 Windows dialogs ................................... 70 5.7 Static HTML output .................................. 70 5.8 Complete graphical interfaces (GUIs) ........................ 72 5.9 Understanding GUI and event-based programming . 73 5.10 PHP-GTK ....................................... 74 5.11 wxPHP ......................................... 76 5.12 Local web server & browser ............................. 77 5.13 PHP’s Built-in testing server ............................. 78 5.14 Web sockets & browser ................................ 79 5.15 SiteFusion ....................................... 81 5.16 Winbinder ....................................... 82 5.17 Adobe AIR ....................................... 83 5.18 Titanium ........................................ 84 5.19 PHP-Qt ........................................ 84 5.20 PHP/TK ........................................ 85 6 System software ...................................... 86 6.1 Daemons in PHP ................................... 86 6.2 Creating a daemon .................................. 87 6.3 Network daemons using libevent ........................... 91 6.4 File monitoring daemons using inotify ........................ 97 6.5 Task dispatch & management systems . 102 6.6 Gearman and PHP ..................................103 6.7 Other task dispatch systems .............................105 7 Interacting with other software ............................. 107 7.1 Starting external processes from PHP, or “shelling out” . 107 7.2 Talking to other processes ..............................108 7.3 Semaphores ......................................108 7.4 Shared Memory ....................................110 7.5 PHP message queues .................................115 7.6 Third party message queues .............................120 7.7 APC cached variables .................................122 7.8 Virtual files - tmpfs ..................................123 7.9 Standard streams ...................................124 7.10 Linux signals .....................................124 CONTENTS 7.11 Task dispatch & management systems . 124 8 Talking to the system ................................... 125 8.1 Filesystem interactions ................................125 8.2 Data files & formats ..................................125 8.3 Dealing with large files ................................126 8.4 Understanding filesystem interactions . 128 8.5 The PHP file status and realpath caches . 129 8.6 Working with cross platform filesystems . 130 8.7 Accessing the Windows Registry . 130 8.8 Linux signals .....................................133 8.9 Linux timed-event signals ..............................136 8.10 Printing (to paper) ...................................139 8.11 Audio .........................................141 8.12 Databases - no change here ..............................143 8.13 Other hardware and system interactions . 143 9 Performance & stability - profiling and improving .................. 144 9.1 The background on performance . 144 9.2 Specific issues for general purpose programming . 145 9.3 Profile, profile, profile! ................................146 9.4 Manual profiling ...................................147 9.5 Profiling tools .....................................148 9.6 Low level profiling ..................................152 9.7 Profiling - the likely results ..............................152 9.8 Silver bullets ......................................153 9.9 Silver bullet #1 - Better hardware . 153 9.10 Silver bullet #2 - Newer PHP versions . 154 9.11 Silver bullet #3 - Opcode caching . 154 9.12 Silver bullet #4 - Compiling ..............................155 9.13 Silver bullet #5 - JIT compilers and alternative Virtual Machines . 157 9.14 The SPL - Standard PHP Library . 161 9.15 Garbage collection ..................................162 9.16 Multi-threading and concurrent programming in PHP . 163 9.17 Big data and PHP - MapReduce . 165 9.18 Data caching .....................................166 9.19 Know thy functions ..................................167 9.20 Outsourcing code to other languages . 168 9.21 Other performance tips and tricks . 169 9.22 Stability and performance of long running processes . 169 9.23 Avoid micro and premature optimisations . 171 10 Distribution and deployment issues ........................... 173 10.1 Error handling and logging ..............................173 10.2 Installers and bundling files .............................175 10.3 Embedded data files at end of PHP script . 175 10.4 Phar executable bundles ...............................176 CONTENTS 10.5 Generic installers ...................................178 10.6 Controlling the (PHP) environment . 178 10.7 Extending your application with plug-ins . 179 10.8 Documentation ....................................180 10.9 Licensing & legal ...................................180 10.10 Deploying frameworks ................................181 11 Where now? or, Thanks & feedback ........................... 183 11.1 Giving feedback, and getting help and support . 183 11.2 Giving testimonials ..................................183 11.3 Are you reading a “pirated” copy? . 183 Appendix A : Compiling and installing PHP and its extensions ............. 185 Compiling and installing PHP itself ............................185 Compiling and installing (extra) core extensions . 188 Installing multiple versions of PHP ............................189 PEAR and PECL .......................................190 Composer ..........................................191 Symfony2 bundles ......................................192 Appendix B : File & data format libraries for PHP ..................... 193 Office documents ......................................193 Compression, archiving & encryption . 194 Graphics ...........................................195 Audio ............................................195 Multimedia & video .....................................196