How phpoffice/phpspreadsheet works
Main Components
• Core API & Data Model: This layer consists of classes representing the spreadsheet’s fundamental structures—Spreadsheet, Worksheet, Cell, Row, Column, DefinedName, and so on. Represent these as “core domain objects.”
• Calculation Engine: A specialized module that handles formulas, functions, calculation settings, and formula parsing. It is subdivided further into areas such as Binary Comparison, Date/Time Excel calculations, Mathematical Functions, Financial, Statistical functions, and text processing. Show multiple sub-components under the Calculation Engine.
• Readers: Modules that handle different file formats (XLS, XLSX, CSV, ODS, HTML, and even specialized XML formats). They convert external spreadsheet file structures into the internal data model.
• Writers: Modules that export or save the data model back to various spreadsheet file formats. There’s a parallel structure to the Readers for CSV, XLS, XLSX, ODS, PDF, and HTML.
• Style, Formatting, and Rendering: Classes responsible for style properties, conditional formatting, fonts, borders, number formats, and even chart components. They reside closely with both the core API and the output writers.
• Helper and Utility Components: These include support for file handling (OLE, zip streams), text and string helpers, trend calculations, and various miscellaneous helpers that are used across the library.
• Testing & Samples: While not part of production runtime, tests (located in the “tests” directory) and sample files (in “samples”) can be shown as separate modules that ensure reliability and also serve as usage examples.
• Documentation & Infrastructure: The “docs” folder contains both user documentation and support for online documentation generators. There is also infrastructure code (e.g., “infra”) used for document generation and locale management.
Relationships and Interactions
• The Core API acts as the heart of the library. Both Reader and Writer modules interact with it—they populate the data model and then persist it.
• The Calculation Engine is used by the Spreadsheet object for processing formulas embedded in cells. It interacts with a Formula Parser, Tokenizer, and a set of specialized function classes.
• Styles and formatting components are applied on top of core cell values and used by the Writers when formatting output.
• Dependency modules, such as those for file reading/ZIP extraction and PHP’s own Composer dependency management (indicated by composer.json) are supporting external libraries and standards (e.g., PSR).
Architectural Patterns and Design Principles
• Layered Architecture: Clearly separate the interface (public API, Readers, Writers) from the core business logic (data model, calculation engine) and supporting utilities.
• Modularity: The project is broken up into self-contained namespaces and modules (e.g., the Calculation sub-modules, the Reader submodules for each file type, etc.) which communicate via well-defined interfaces.
• Separation of Concerns: Distinct modules handle file I/O (Readers/Writers), business logic (Calculation Engine, Core Models), and formatting/styling. This is evident in the directory structure, with a “src/PhpSpreadsheet” root.
• Test-Driven: The comprehensive “tests” directory shows the emphasis on unit tests and integration tests for each module.
Relevant Technologies and Frameworks
• PHP (language with modern features, namespaces, and PSR standards).
• Composer for dependency management.
• Standard PHP testing frameworks (e.g., PHPUnit) as reflected by the tests and configuration files (phpunit.xml.dist, phpstan, etc.).
• Integration with documentation tools (readthedocs and mkdocs configuration files).
Final Touches
• Optionally, include an “External Interfaces” section at the bottom for documentation links, GitHub workflows, and support interactions (StackOverflow, Gitter).