Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 41
Folder Structure
ОглавлениеIn professional development, it's important to create a folder structure that is understandable at a glance and easy to maintain. Most plugin developers will create separate folders to house their PHP code apart from resources, assets, or other front‐end code in a folder with a name such as /src
, /inc
, /includes
, or /app
. Within the larger PHP development community, it is standard practice to name this folder /src
because this is the folder where your “source” code lives.
The naming of the assets or resources folder is also varied with plugins. Often this folder is named /assets
, /dist
, or /public
. Most likely, you'll want to have separate development and production folders if you use a build system such as webpack to bundle your final asset files.
The following is an example of how a plugin folder structure may be organized. Some of the files in this list are covered later in this chapter.
plugin.php: Primary plugin file
uninstall.php: Uninstall file
/src: PHP source codeActivator.php: Activation classDeactivator.php: Deactivation classPlugin.php: Primary plugin class
/resources: Development asset files
/css: Development CSS/js: Development JavaScript
/public: Production asset files/css: Production CSS/js: Production JavaScript
This is a clean structure that will help you maintain your plugin code over time. You may choose a different structure for your own projects.
NOTE The most important thing is consistency. In a professional environment, more than one developer will often be working on code, so it's important that your team understands where to find and edit code.
When using namespaces as described in the “Namespace Everything” section of this chapter, it is standard practice to have a folder structure that matches how your namespaces and subnamespaces are set up so that it is easy to autoload the files using a system that follows the PHP‐FIG autoloading standard (https://www.php-fig.org/psr/psr-4
).
With a fully qualified class name of PDEV\Post\Edit
, you'd have the following structure within your /src
folder:
/Post: SubnamespaceEdit.php: Class file
Following this structure will tie your code and namespace structure directly to your folder structure. It makes it simple for you and other developers to navigate your project's files and folders by just looking at the namespace and class name.
This is the standard used by the larger PHP community and has been for many years. Much of the WordPress community is still light‐years behind in following this standard, but it is beginning to catch on with more and more plugin developers.