Zend Enterprise PHP Patterns
Total Page:16
File Type:pdf, Size:1020Kb
Zend Enterprise PHP Patterns John Coggeshall with Morgan Tocker [email protected] Zend Enterprise PHP Patterns Copyright © 2009 by John Coggeshall All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-1974-3 ISBN-13 (electronic): 978-1-4302-1975-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Matt Wade Technical Reviewer: Kevin Schroeder Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Managers: Sofia Marchant and Kylie Johnston Copy Editor: Hastings Hart Associate Production Director: Kari Brooks-Copony Production Editor: April Eddy Compositor: Lynn L’Heureux Proofreader: Liz Welch Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail kn`ano)ju<olnejcan)o^i*_ki, or visit dppl6++sss*olnejcankjheja*_ki. For information on translations, please contact Apress directly at 233 Spring Street, New York, NY 10013. E-mail ejbk<]lnaoo*_ki or visit dppl6++sss*]lnaoo*_ki. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at dppl6++sss*]lnaoo*_ki+ejbk+^qhgo]hao. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indi- rectly by the information contained in this work. The source code for this book is available to readers at dppl6++sss*]lnaoo*_ki. [email protected] To my daughter, Diana Katheryn Coggeshall, who continues to be the single steadfast reason I have to do more and be more in my life John Coggeshall [email protected] Contents at a Glance Foreword ........................................................................xiii About the Authors.................................................................xv About the Technical Reviewer ..................................................... xvii Acknowledgments ................................................................xix Introduction ......................................................................xxi CHAPTER 1 Introduction to Zend Framework .................................1 CHAPTER 2 Introduction to Zend Studio for Eclipse ..........................33 CHAPTER 3 Web Application Performance and Analysis......................57 CHAPTER 4 Data-Caching Strategies in PHP.................................71 CHAPTER 5 Asynchronous Operations with PHP .............................93 CHAPTER 6 Securing Your PHP Applications................................123 CHAPTER 7 Monitoring Your Applications ..................................141 CHAPTER 8 Web Services and Zend Framework ............................167 CHAPTER 9 Production Farms for PHP .....................................193 CHAPTER 10 The MySQL Database..........................................211 INDEX .......................................................................249 v [email protected] Contents Foreword ........................................................................xiii About the Authors.................................................................xv About the Technical Reviewer ..................................................... xvii Acknowledgments ................................................................xix Introduction ......................................................................xxi CHAPTER 1 Introduction to Zend Framework............................1 Introduction to Zend Framework Library .............................1 Zend Framework MVC ............................................3 Model, View, and Controller ...................................3 “Hello World” in Zend Framework .............................9 Zend Framework Request/Response Objects and Error Handling.... 27 Conclusion .....................................................31 CHAPTER 2 Introduction to Zend Studio for Eclipse ...................33 Getting Started Zend Studio for Eclipse.............................33 Creating Projects in Zend Studio for Eclipse ....................36 Debugging in Zend Studio for Eclipse..........................38 Profiling in Zend Studio for Eclipse ............................52 Conclusion .....................................................55 CHAPTER 3 Web Application Performance and Analysis ..............57 Locating the Bottleneck ..........................................59 How Are You Bound?........................................59 Using vmstat ...............................................59 Determining Whether You Are CPU-Bound .....................62 Determining Whether You Are Memory-Bound..................63 Determining Whether You Are I/O-Bound ......................65 vii [email protected] viii NCONTENTS Where to Start Looking ...........................................66 CPU Bounding Influence .....................................66 Memory Bounding Influence .................................67 I/O Bounding Influence ......................................67 When the Bottleneck Is a Remote Procedure Call ...............68 Simulating Load to Identify Future Bottlenecks .................68 Conclusion .....................................................70 CHAPTER 4 Data-Caching Strategies in PHP ...........................71 Opcode Caching.................................................71 The PHP Execution Cycle ....................................71 Full-Page Caching ...............................................74 What Is Full-Page Caching? ..................................74 Setting Up Full-Page Caching ................................76 Final Thoughts on Full-Page Caching..........................79 Semi-Full-Page Caching..........................................79 Programmatic Caching ...........................................82 Components of Caching in Zend Framework ...................82 You Can (Almost) Always Cache Things........................83 Knowing Your Cache Effectiveness............................85 The Various Zend_Cache Back Ends ..........................85 Conclusion .....................................................92 CHAPTER 5 Asynchronous Operations with PHP .......................93 Getting Started with Job Queue ...................................95 Advanced Job Queue Configuration ................................97 Replacing Job Queue’s PHP with Your Own ....................97 Modifying the Configuration of Job Queue’s PHP................99 Controlling the Job Queue from the Command Line .............99 Using the Job Queue to Execute PHP Scripts .......................100 Creating Your First Job .....................................100 Searching for Existing Jobs .................................102 Using Input Parameters in Job Queue Scripts..................105 Creating Jobs Programmatically Using the Job Queue API.......108 Conclusion ....................................................121 [email protected] NCONTENTS ix CHAPTER 6 Securing Your PHP Applications ..........................123 Setting the Context .............................................123 Defining Security ...............................................124 Common Threats and Defenses ..................................124 Input Security .............................................125 Securing File Uploads ......................................130 Output Security............................................135 Conclusion ....................................................138 CHAPTER 7 Monitoring Your Applications .............................141 Effective Logging Through Zend_Log .............................141 Getting Started with Zend_Log ..............................142 Advanced Monitoring ...........................................149 What Is PHP Intelligence?...................................149 Getting Started with PHP Intelligence.........................150 Creating Advanced Monitoring Facilities ......................155 Customizing Zend_Log Behavior and Integrating ..............159 with Zend Platform Logging and Performance .......................................165 Conclusion ....................................................166 CHAPTER 8 Web Services and Zend Framework ......................167 The Multi-Transport Services Architecture Using ZF.................167 The Command Pattern .....................................168