Zend Framework ; Vol. 2 of 2 · Zend_Log 95 Overview 95 CreatingaLog 95 LoggingMessages 95...
Transcript of Zend Framework ; Vol. 2 of 2 · Zend_Log 95 Overview 95 CreatingaLog 95 LoggingMessages 95...
Zend Framework
The Official Programmer's Reference Guide
Volume 2 of 2
Zend, Inc.
TECHNISCHE
INFORMATIONSBIBUOTHEK
UN1VERSITATSBIBLIOTHEKHANNOVER
Apress®
The Zend Press
Series
TIB/UB Hannover 89
133 299 988
Programmer's Reference Guide: Volume 2
Manipulating Plugin Paths 45
Testing for Plugins and Retrieving Class Names 45
Getting Better Performance for Plugins 456. Zend_Locale 47
Introduction ..47
What is Localization 47
What is a Locale? 48
How are Locales Represented? 48
Selecting the Right Locale 49
Usage of automatic Locales 49
Using a default Locale 50
ZF Locale-Aware Classes 51
Application wide locale 52
Zend_Locale_Format::setOptions(array Soptions) 52
Speed up Zend_Locale and its subclasses 53
Using Zend_Locale 53
Copying, Cloning, and Serializing Locale Objects 53
Equality 53
Default locales 54
Set a new locale 54
Getting the language and region 55
Obtaining localized strings 55
Obtaining translations for "yes" and "no" 67
Get a list of all known locales 68
Detecting locales 68
Normalization and Localization 70
Number normalization: getNumber($input, Array Soptions) 70
Number localization 71
Number testing 73
Float value normalization 73
Floating point value localization 74
Floating point value testing 74
Integer value normalization 74
Integer point value localization 74
Integer value testing 75
Numeral System Conversion 75
Working with Dates and Times 77
Normalizing Dates and Times 77
Testing Dates 81
Normalizing a Time 81
Testing Times 81
Supported locales 82
Migrating from previous versions 93
Migrating from 1.6 to 1.7 or newer 93
7. Zend_Log 95
Overview 95
Creating a Log 95
Logging Messages 95
Destroying a Log 96
Using Built-in Priorities 96
Adding User-defined Priorities 96
Understanding Log Events 96
Writers 97
Writing to Streams 97
Writing to Databases 98
Writing to Firebug 98
Writing to Email 100
Stubbing Out the Writer 102
v
Programmer's Reference Guide: Volume 2
Testing with the Mock 102
Compositing Writers 103
Formatters 103
Simple Formatting 103
Formatting to XML 104
Filters 104
Filtering for All Writers 104
Filtering for a Writer Instance 105
8.Zend_Mail 107
Introduction 107
Getting started 107
Configuring the default sendmaii transport 107
Sending via SMTP 108
Sending Multiple Mails per SMTP Connection 108
Using Different Transports 109
HTML E-Mail HO
Attachments HO
Adding Recipients 111
Controlling the MIME Boundary 111
Additional Headers 112
Character Sets 112
Encoding 112
SMTP Authentication 112
Securing SMTP Transport 113
Reading Mail Messages 113
Simple example using Pop3 114
Opening a local storage114
Opening a remote storage114
Fetching messages and simple methods 115
Working with messages116
Checking for flags 118
Using folders 119
Advanced Use 120
9. Zend_Measure 125
Introduction 125
Creation of Measurements 125
Creating measurements from integers and floats 125
Creating measurements from strings 126
Measurements from localized strings 127
Outputting measurements 127
Automatic output 128
Outputting values 128
Output with unit ofmeasurement 128
Output as localized string 129
Manipulating Measurements 129
Convert 129
Add and subtract 129
Compare 130
Compare 131
Manually change values 131
Manually change types 132
Types of measurements 132
Hints for Zend_Measure_Binary 135
Hints for Zend_Measure_Number 135
Roman numbers 135
10. Zend_Memory 137
Overview 137
Introduction 137
vi
Programmer's Reference Guide: Volume 2
Theory of Operation 137
Memory Manager 138
Creating a Memory Manager 138
Managing Memory Objects 139
Memory Manager Settings 140
Memory Objects 140
Movable 140
Locked 140
Memory container 'value' property 141
Memory container interface 141
11. Zend_Mime 145
Zend_Mime 145
Introduction 145
Static Methods and Constants 145
Instantiating Zend_Mime 145
Zend_Mime_Message 146
Introduction 146
Instantiation 146
Adding MIME Parts 146
Boundary handling 146
parsing a string to create a Zend_Mime_Message object (experimental) 146
Zend_Mime_Part 147
Introduction 147
Instantiation 147
Methods for rendering the messagepart to a string 147
12. ZendJMavigation 149
Introduction •149
Pages and Containers 149
Separation ofdata (model) and rendering (view) 149
Pages 149
Common page features 149
Zend_Navigation_Page_Mvc 152
Zend_Navigation_Page_Uri 155
Creating custom page types 155
Creating pages using the page factory 157
Containers 158
Creating containers 158
Adding pages164
Removing pages165
Finding pages •166
Iterating containers 167
Other operations 168
13. Zend_OpenId 171
Introduction 171
What is OpenID? 171
How Does it Work? 171
Zend_OpenId Structure 172
Supported OpenID Standards 172
Zend_OpenId_Consumer Basics 172
OpenID Authentication 172
Combining all Steps in One Page 174
Consumer Realm 175
Immediate Check 175
Zend_OpenId_Consumer_Storage 175
Simple Registration Extension 179
Integration with Zend_Auth 181
Integration with Zend_Controller 182
Zend_OpenId_Provider 182
vii
Programmer's Reference Guide: Volume 2
Quick Start 182
Combined Provide Scripts • 185
Simple Registration Extension 186
Anything Else? 188
14. Zend_Paginator 189
Introduction 189
Usage 189
Paginating data collections 189
Rendering pages with view scripts 190
Configuration 195
Advanced usage 195
Custom data source adapters 195
Custom scrolling styles 196
Caching features 196
15. Zend_Pdf 199
Introduction 199
Creating and Loading PDF Documents 199
Save Changes to PDF Documents 200
Working with Pages 200
Page Creation 201
Page cloning 201
Drawing 202
Geometry 202
Colors 202
Shape Drawing 203
Text Drawing 205
Using fonts 205
Standard PDF fonts limitations 208
Extracting fonts 209
Image Drawing 210
Line drawing style 211
Fill style 211
Linear Transformations 212
Save/restore graphics state 213
Clipping draw area 214
Styles 215
Transparency 217
Document Info and Metadata 217
Zend_Pdfmodule usage example 219
16. Zend_ProgressBar 225
Zend_ProgressBar 225
Introduction 225
Basic Usage of Zend_Progressbar 225
Persistent progress 225
Standard adapters 225
17. Zend_Reflection 229
Introduction 229
Zend_Reflection Examples 229
Zend_Reflection Reference 231
Zend_Reflection_Docblock 231
Zend_Reflection_Docblock_Tag 231
Zend_Reflection_Docblock_Tag_Param 232
Zend_Reflection_Docblock_Tag_Return 232
Zend_Reflection_File 232
Zend_Reflection_Class 232
Zend_Reflection_Extension 233
Zend_Reflection_Function 233
Zend_Reflection_Method 233
viii
Programmer's Reference Guide: Volume 2
Zend_Reflection_Parameter 233ZendJleflectionJProperty ....233
18. Zend_Registry 235Using the Registry 235
Setting Values in the Registry 235Getting Values from the Registry 235Constructing a Registry Object 235
Accessing the Registry as an Array 236
Accessing the Registry as an Object 236
Querying if an Index Exists 237
Extending the Registry 238
Unsetting the Static Registry 23819. ZendRest 239
Introduction 239
Zend_Rest_Client 239Introduction 239
Responses 239Request Arguments 241
Zend_Rest_Server 242Introduction 242REST Server Usage 242
Calling a Zend_Rest_Server Service 242
Sending A Custom Status 242Returning Custom XML Responses 243
20. Zend_Search_Lucene 245Overview 245
Introduction 245Document and Field Objects 245
Understanding Field Types 246HTML documents 247Word 2007 documents 248
Powerpoint 2007 documents 249Excel 2007 documents 250
Building Indexes 251
Creating a New Index 251
Updating Index 251
Updating Documents 251
Retrieving Index Size 252
Index optimization 252
Permissions 253
Limitations 254
Searching an Index 254
Building Queries 254
Search Results 255
Limiting the Result Set 256
Results Scoring 256
Search Result Sorting 257
Search Results Highlighting 257
Query Language 259
Terms 259
Fields 260
Wildcards 260
Term Modifiers 261
Range Searches 261
Fuzzy Searches 262
Matched terms limitation 262
Proximity Searches 262
Boosting a Term 262
ix
Programmer's Reference Guide: Volume 2
Boolean Operators 263
Grouping 264
Field Grouping 264
Escaping Special Characters 264
Query Construction API 265
Query Parser Exceptions 265
Term Query 265
Multi-Term Query 266
Boolean Query 267
Wildcard Query 268
Fuzzy Query 269
Phrase Query 269
Range Query 272
Character Set 272
UTF-8 and single-byte character set support 272
Default text analyzer 273
UTF-8 compatible text analyzers 273
Extensibility 274
Text Analysis 274
Tokens Filtering 276
Scoring Algorithms 277
Storage Containers 278
Interoperating with JavaLucene 280
File Formats 280
Index Directory 281
Java Source Code 281
Advanced 281
Starting from 1.6, handling index format transformations 281
Using the index as static property 282
Best Practices 283
Fieldnames 283
Indexing performance 284
Index during Shut Down 285
Retrieving documents by unique id 286
Memory Usage 287
Encoding 287
Index maintenance 288
21, Zend_Server 289
Introduction 289
Zend_Server_Refiection 289
Introduction 289
Usage 289
22. Zend_Service 291
Introduction 291
Zend_Service_Akismet 291
Introduction 291
Verify an API key 291
Check for spam 292
Submitting known spam 293
Submitting false positives (ham) 293
Zend-specific Accessor Methods 294
Zend_Service_Amazon 294
Introduction 294
Country Codes 295
Looking up a Specific Amazon Item by ASIN 295
Performing Amazon Item Searches 296
Using the Alternative Query API 297
Zend Service Amazon Classes 297
x
Programmer's Reference Guide: Volume 2
Zend_Service_Amazon_Ec2 302
Introduction 302
What is Amazon Ec2? 302
Zend_Service_Amazon_Ec2: Instances 302Instance Types 302
Running Amazon EC2 Instances 304Amazon Instance Utilities 305
Zend_Service_Amazon_Ec2: Amazon Machine Images (AMI) 307
AMI Information Utilities 307AMI Attribute Utilities 308
Zend_Service_AmazonJEc2: Elastic Block Stroage (EBS) 309Create EBS Volumes and Snapshots 310
Describing EBS Volumes and Snapshots 311Attach and Detaching Volumes from Instances 311Deleting EBS Volumes and Snapshots 312
Zend_Service_Amazon_Ec2: Elastic IP Addresses 313
Zend_Service_Amazon_Ec2: Keypairs, 314
Zend_Service_Amazon_Ec2: Regions and Availability Zones 315Amazon EC2 Regions 315
Amazon EC2 Availability Zones 315
Zend_Service_Amazon_Ec2: Security Groups 316
Security Group Maintenance 316
Authorizing Access 317
Revoking Access 318
Zend_Service_Amazon_S3 319Introduction 319
Registering with Amazon S3 319API Documentation 319Features 319
Getting Started 319Bucket operations 320
Object operations 321Stream wrapper 322
Zend_Service_Audioscrobbler 323
Introduction 323
Users 323
Artists 325
Tracks 326
Tags 326
Groups 326
Forums 326
Zend_Service_Delicious 327
Introduction 327
Retrieving posts 327
Zend_Service_Delicious_PostList 328
Editing posts 329
Deleting posts 329
Adding new posts 330
Tags 330
Bundles 330
Public data 331
HTTP client 332
Zend_Service_Flickr 332
Introduction 332
Finding Flickr Users' Photos and Information 333
Finding photos From a Group Pool 333
Retrieving Flickr Image Details 334
Zend_Service_Flickr Result Classes 334
xi
Programmer's Reference Guide: Volume 2
Zend_Service_Nirvanix 336
Introduction 336
Registering with Nirvanix 336
API Documentation 336
Features 336
Getting Started 337
Understanding the Proxy 337
Examining Results 338
Handling Errors 339
Zend_Service_ReCaptcha 339
Introduction 339
Simplest use 340
Zend_Service_Simpy 340
Introduction 340
Links 341
Tags 342
Notes 343
Watchlists 344
Introduction 344
Getting Started with Zend_Service_SlideShare 345
The SlideShow object 345
Retrieving a single slide show 347
Retrieving Groups ofSlide Shows 348
Zend_Service__SlideShare Caching policies 348
Changing the behavior of the HTTP Client 349
Zend_Service_StrikeIron 349
Overview 349
Registering with Strikelron 350
Getting Started 350
Making Your First Query 351
Examining Results 351
Handling Errors 352
Checking Your Subscription 353
Zend_Service_StrikeIron: Bundled Services 353
ZIP Code Information 353
U.S. Address Verification 354
Sales & Use Tax Basic 355
Zend_Service_StrikeIron: Advanced Uses 355
Using Services by WSDL 355
Viewing SOAP Transactions 356
Zend_Service_Technorati 356
Introduction 356
Getting Started 357
Making Your First Query 357
Consuming Results 358
Handling Errors 359
Checking Your API Key Daily Usage 360
Available Technorati Queries 360
Zend_Service_Technorati Classes 364
Zend_Service_Twitter 367
Introduction 367
Authentication 368
Account Methods 368
Status Methods 369
User Methods 371
Direct Message Methods 372
Friendship Methods 373
Favorite Methods 373
xii
Programmer's Reference Guide: Volume 2
Zend_Service_Twitter_Search 374
Zend_Service_Yahoo 375
Introduction 375
Searching the Web with Yahoo! 375
Finding Images with Yahoo! 376
Finding videos with Yahoo! 376
Finding Local Businesses and Services with Yahoo! 377
Searching Yahoo! News 377
Searching Yahoo! Site Explorer Inbound Links 377
Searching Yahoo! Site Explorer's PageData 377
Zend_Service_Yahoo Classes 378
23. Zend_Session 385
Introduction 385
Basic Usage ,385
Tutorial Examples 386
Iterating Over Session Namespaces 387
Accessors for Session Namespaces 387
Advanced Usage 387
Starting a Session 388
Locking Session Namespaces 389
Namespace Expiration 389
Session Encapsulation and Controllers 390
Preventing Multiple Instances per Namespace 390
Working with Arrays 391
Using Sessions with Objects 392
Using Sessions with Unit Tests , 392
Global Session Management 393
Configuration Options 394
Error: Headers Already Sent 396
Session Identifiers 396
rememberMe(integer $seconds) 398
forgetMeQ 398
sessionExists() 398
destroy(bool $remove_cookie = true, boot Sreadonly = true) 398
stop() 399
writeClose($readonly = true) 399
expireSessionCookie() 399
setSaveHandler(Zend_Session_SaveHandler_Interface$interface) 399
namespacelsset($namespace) 399
namespaceUnset($namespace) 399
namespaceGet($namespace) 400
getIterator() 400
Zend_Session_SaveHandler_DbTable 400
24. Zend_Soap 403
Zend_Soap_Server 403
Zend_Soap_Server constructor 403
Methods to define Web Service API 404
Request and response objects handling 405
Zend_Soap_Client 406
Zend_Soap_Client Constructor 406
Performing SOAP Requests 407
WSDL Accessor 408
Zend_Soap_Wsdl constructor 408
addMessage() method 409
addPortType() method 409
addPortOperation() method 409
addBinding() method 410
addBindingOperationQ method 410
xiii
Programmer's Reference Guide: Volume 2
addSoapBinding() method 410
addSoapOperation() method 410
addService() method 411
Type mapping 411
addDocumentation() method 413
Get finalized WSDL document 413
Parsing WSDL documents 413
AutoDiscovery 414
AutoDiscovery Introduction 414
Class autodiscovering 415
Functions autodiscovering 416
Autodiscovering Datatypes 417
WSDL Binding Styles 417
25. Zend_Tag 419
Introduction 419
Zend_Tag_Cloud 419
Decorators 420
26. Zend_Test 423
Introduction 423
Zend_Test_PHPUnit 423
Bootstrapping your TestCase 424
Testing your Controllers and MVC Applications 425
Assertions 426
Examples 429
27. Zend_Text 433
Zend_Text_Figlet 433
Zend_Text_Table 434
28. Zend_TimeSync 437
Introduction 437
Why Zend_TimeSync ? 437
WhatisNTP? 437
What is SNTP? 438
Problematic usage438
Decide which server to use 438
Working with ZendJTimeSync 438
Generic Time Server Request 438
Multiple Time Servers 439
Protocols ofTime Servers 439
Using Ports for Time Servers 440
Time Servers Options 440
Using Different Time Servers 440
Information from Time Servers 441
Handling Exceptions 441
29. Zend_Tool_Framework 443
Introduction 443
Using the CLI Tool 443
Setting up the CLI tool 444
Setting up the CLI tool on Unix-like Systems 444
Setting up the CLI tool on Windows 445
Other Setup Considerations 446
Where To Go Next? 447
Architecture 447
Registry 447
Providers 448
Loaders 449
Manifests 449
Clients 451
Creating Providers to use with Zend_Tool_Framework 452
xiv
Programmer's Reference Guide: Volume 2
Basic Instructions for Creating Providers 452
Advanced Development Information 453
Shipped System Providers 453The Version Provider 454The Manifest Provider 454
30. Zend_Tool_Project 455
Zend_Tool_Project Introduction 455Create A Project 455Zend Tool Project Providers 456
31. ZendJTranslate 457Introduction 457
Starting multi-lingual 457
Adapters for ZendJTranslate 458How to decide which translation adapter to use 458
Integrate self written Adapters ,460
Speedup all Adapters 461
Using Translation Adapters 461Translation Source Structures 463
Creating Array source files 464
Creating Gettext source files 465
Creating TMX source files 465
Creating CSV source files 466
Creating INI source files 467
Options for adapters 467
Handling languages 469
Automatic source detection 471
Checking for translations 474
How to log not found translations 475
Accessing source data 476
Migrating from previous versions 477
Migrating from 1.6 to 1.7 or newer 477
32. ZendJJri 479
ZendJJri 479
Overview 479
Creating a New URI 479
Manipulating an Existing URI 479
URI Validation 480
Common Instance Methods 480
33. ZendJValidate 483
Introduction 483
What is a validator? 483
Basic usage of validators 483
Customizing messages 484
Using the static is() method 485
Translating messages 485
Standard Validation Classes 486
Alnum 486
Alpha 486
Barcode 486
Between 486
Ccnum 486
Date 486
Db_RecordExists and Db_NoRecordExists 487
Digits 488
EmailAddress 488
Float 490
GieaterThan 490
Hex 490
xv
Programmer's Reference Guide: Volume 2
Hostname 490
Iban 492
InArray 493
Int 493
Ip 493
LessThan 493
NotEmpty 493
Regex 493
Sitemap Validators 493
StringLength 494
Validator Chains 494
Writing Validators 495
Validation Messages 499
34. Zend_Version 507
Getting the Zend Framework Version 507
35. Zend_View 509
Introduction 509
Controller Script 509
View Script 509
Options 510
Short Tags with View Scripts 510
Utility Accessors 511
Controller Scripts 511
Assigning Variables 512
Rendering a View Script 512
View Script Paths 513
View Scripts 513
Escaping Output 514
Using Alternate Template Systems 515
View Helpers 520
Initial Helpers 520
Helper Paths 567
Writing Custom Helpers 567
Zend_View_Abstract 569
Migrating from Previous Versions 569
Migrating from versions prior to 1.7.5 569
36. Zend_Wildfire 571
Zend_Wildfire 571
37. Zend_XmlRpc 573
Introduction 573
Zend_XmlRpc_Client 573
Introduction 573
Method Calls 573
Types and Conversions 574
Server Proxy Object 575
Error Handling 576
Server Introspection 577
From Request to Response 577
HTTP Client and Testing 578
Zend_XmlRpc_Server 578
Introduction 578
Basic Usage 578
Server Structure 579
Conventions 579
Utilizing Namespaces 580
Custom Request Objects 580
Custom Responses 580
Handling Exceptions via Faults 580
xvi
Programmer's Reference Guide: Volume 2
Caching Server Definitions Between Requests 581
Usage Examples 581
A. Zend Framework Requirements 585
Introduction 585
PHP Version 585
PHP Extensions 585
ZendFramework Components 589
Zend Framework Dependencies 592
B. Zend Framework Coding Standard for PHP 617
Overview 617
Scope 617
Goals 617
PHP File Formatting 617
General 617
Indentation 617
Maximum Line Length 617
Line Termination 617
Naming Conventions 618
Classes, 618
Filenames 618
Functions and Methods 618
Variables 619
Constants 619
Coding Style 620
PHP Code Demarcation 620
Strings 620
Arrays 621
Classes 621
Functions and Methods 622
Control Statements 624
Inline Documentation 625
C. Zend Framework Performance Guide 627
Introduction 627
Class Loading 627
How can I optimize my include_path? 627
How can I eliminate unnecessary require_once statements? 629
How can I speed up plugin loading? 629
Zend_Db Performance 630
How can I reduce overhead introducedby Zend_Db_Table for retrieving table
metadata? 630
SQL generated with Zend_Db_Select s not hitting my indexes; how can I make it
better? 631
Internationalization (il8n) and Localization (HOn) 631
Which translation adapter should I use? 631
How can I make translation and localization even faster? 632
View Rendering 632
How can I speed up resolution ofview helpers? 632
How can I speed up view partials? 633
How can I speed up calls to the action() view helper? 634
D. Copyright Information 637
Index •639
xvii