1
1
Fork 0

Add new documentation page about moving classes to src

This commit is contained in:
Hypolite Petovan 2017-04-30 17:34:23 -04:00
parent 70563e0324
commit dfd4c8528e
7 changed files with 222 additions and 101 deletions

View file

@ -113,3 +113,8 @@ For Composer, this would be:
```` ````
$> COMPOSER_HOME=/var/tmp/composer sudo -u [web user] util/composer.phar [mode] $> COMPOSER_HOME=/var/tmp/composer sudo -u [web user] util/composer.phar [mode]
```` ````
## Related
* [Class autoloading](help/autoloader)
* [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)

View file

@ -0,0 +1,105 @@
How To Move Classes to `src`
==============
* [Home](help)
* [Developer Intro](help/Developers-Intro)
Since April 2017, Friendica uses [Composer](help/Composer) to manage autoloading.
This means that all the PHP class files moved to the `src` folder will be [automatically included](help/autoloader) when the class it defines is first used in the flow.
This is an improvement over the current `require` usage since files will be included on an actual usage basis instead of the presence of a `require` call.
However, there are a significant number of items to check when moving a class file from the `include` folder to the `src` folder, and this page is there to list them.
## Decide the namespace
This isn't the most technical decision of them all, but it has long lasting consequences as it will be the name that will be used to refer to this class from now on.
There is [a shared Ethercalc sheet](https://ethercalc.org/friendica_classes) to suggest namespace/class names that lists all the already moved class files for inspiration.
A few pointers though:
* `Friendica` is the base namespace for all classes in the `src` folder
* Namespaces match the directory structure, with `Friendica` namespace being the base `src` directory. The `Config` class set in the `Friendica\Core` namespace is expected to be found at `src/Core/Config.php`.
* Namespaces can help group classes with a similar purpose or relevant to a particular feature
When you're done deciding the namespace, it's time to use it.
Let's say we choose `Friendica\Core` for the `Config` class.
## Use the namespace
To declare the namespace, the file `src/Core/Config.php` must start with the following statement:
````php
namespace Friendica\Core;
````
From now on, the `Config` class can be referred to as `Friendica\Core\Config`, however it isn't very practical, especially when the class was previously used as `Config`.
Thankfully, PHP provides namespace shortcuts through `use`.
This language construct just provides a different naming scheme for a namespace or a class, but doesn't trigger the autoload mechanism on its own.
Here are the different ways you can use `use`:
````php
// No use
$config = new Friendica\Core\Config();
````
````php
// Namespace shortcut
use Friendica\Core;
$config = new Core\Config();
````
````php
// Class name shortcut
use Friendica\Core\Config;
$config = new Config();
````
````php
// Aliasing
use Friendica\Core\Config as Cfg;
$config = new Cfg();
````
Whatever the style chosen, a repository-wide search has to be done to find all the class name usage and either use the fully-qualified class name (including the namespace) or add a `use` statement at the start of each relevant file.
## Escape non-namespace classes
The class file you just moved is now in the `Friendica` namespace, but it probably isn't the case for all the classes referenced in this file.
Since we added a `namespace Friendica\Core;` to the file, all the class names still declared in `include` will be implicitly understood as `Friendica\Core\ClassName`, which is rarely what we expect.
To avoid `Class Friendica\Core\ClassName not found` errors, all the `include`-declared class names have to be prepended with a `\`, it tells the autoloader not to look for the class in the namespace but in the global space where non-namespaced classes are set.
If there are only a handful of references to a single non-namespaced class, just prepending `\` is enough. However, if there are many instance, we can use `use` again.
````php
namespace Friendica\Core;
...
if (\dbm::is_result($r)) {
...
}
````
````php
namespace Friendica\Core;
use \dbm;
if (dbm::is_result($r)) {
...
}
````
## Remove any useless `require`
Now that you successfully moved your class to the autoloaded `src` folder, there's no need to include this file anywhere in the app ever again.
Please remove all the `require_once` mentions of the former file, as they will provoke a Fatal Error even if the class isn't used.
## Miscellaneous tips
When you are done with moving the class, please run `php util/typo.php` from the Friendica base directory to check for obvious mistakes.
Howevever, this tool isn't bullet-proof, and a staging install of Friendica is recommended to test your class move without impairing your production server if you host one.
Most of Friendica processes are run in the background, so make sure to turn on your debug log to check for errors that wouldn't show up while simply browsing Friendica.
## Related
* [Class autoloading](help/autoloader)
* [Using Composer](help/Composer)

View file

@ -52,7 +52,9 @@ Friendica uses [Composer](https://getcomposer.org) to manage dependencies librar
It's a command-line tool that downloads required libraries into the `vendor` folder and makes any namespaced class in `src` available through the whole application through `boot.php`. It's a command-line tool that downloads required libraries into the `vendor` folder and makes any namespaced class in `src` available through the whole application through `boot.php`.
* [Class autoloading](help/autoloader)
* [Using Composer](help/Composer) * [Using Composer](help/Composer)
* [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)
###Coding standards ###Coding standards

View file

@ -36,21 +36,25 @@ Friendica Documentation and Resources
**Developer Manual** **Developer Manual**
* [Where to get started?](help/Developers-Intro) * [Get started](help/Developers-Intro)
* [Help on Github](help/Github) * Set up development environment
* [Help on Vagrant](help/Vagrant) * [Help on Github](help/Github)
* [How to translate Friendica](help/translations) * [Help on Vagrant](help/Vagrant)
* [Bugs and Issues](help/Bugs-and-Issues) * [Bugs and Issues](help/Bugs-and-Issues)
* [Plugin Development](help/Plugins) * Code structure
* [Theme Development](help/themes) * [Plugin Development](help/Plugins)
* [Smarty 3 Templates](help/smarty3-templates) * [Theme Development](help/themes)
* [Protocol Documentation](help/Protocol) * [Smarty 3 Templates](help/smarty3-templates)
* [Database schema documantation](help/database) * How To
* [Class Autoloading](help/autoloader) * [Translate Friendica](help/translations)
* [Using Composer](help/Composer) * [Use Composer](help/Composer)
* [Code - Reference(Doxygen generated - sets cookies)](doc/html/) * [Move classes to `src`](help/Developer-How-To-Move-Classes-to-src)
* [Twitter/GNU Social API Functions](help/api) * Reference
* [Twitter/GNU Social API Functions](help/api)
* [Code (Doxygen generated - sets cookies)](doc/html/)
* [Protocol Documentation](help/Protocol)
* [Database schema documantation](help/database)
* [Class Autoloading](help/autoloader)
**External Resources** **External Resources**

View file

@ -24,14 +24,14 @@ For more info about PHP autoloading, please refer to the [official PHP documenta
Let's say you have a PHP file in `src/` that define a very useful class: Let's say you have a PHP file in `src/` that define a very useful class:
```php ```php
// src/ItemsManager.php // src/ItemsManager.php
<?php <?php
namespace \Friendica; namespace Friendica;
class ItemsManager { class ItemsManager {
public function getAll() { ... } public function getAll() { ... }
public function getByID($id) { ... } public function getByID($id) { ... }
} }
``` ```
The class `ItemsManager` has been declared in the `Friendica` namespace. The class `ItemsManager` has been declared in the `Friendica` namespace.
@ -43,16 +43,16 @@ In order for the Composer autoloader to work, it must first be included. In Frie
The code will be something like: The code will be something like:
```php ```php
// mod/network.php // mod/network.php
<?php <?php
function network_content(App $a) { function network_content(App $a) {
$itemsmanager = new \Friendica\ItemsManager(); $itemsmanager = new Friendica\ItemsManager();
$items = $itemsmanager->getAll(); $items = $itemsmanager->getAll();
// pass $items to template // pass $items to template
// return result // return result
} }
``` ```
That's a quite simple example, but look: no `require()`! That's a quite simple example, but look: no `require()`!
@ -61,132 +61,137 @@ If you need to use a class, you can simply use it and you don't need to do anyth
Going further: now we have a bunch of `*Manager` classes that cause some code duplication, let's define a `BaseManager` class, where we move all common code between all managers: Going further: now we have a bunch of `*Manager` classes that cause some code duplication, let's define a `BaseManager` class, where we move all common code between all managers:
```php ```php
// src/BaseManager.php // src/BaseManager.php
<?php <?php
namespace \Friendica; namespace Friendica;
class BaseManager { class BaseManager {
public function thatFunctionEveryManagerUses() { ... } public function thatFunctionEveryManagerUses() { ... }
} }
``` ```
and then let's change the ItemsManager class to use this code and then let's change the ItemsManager class to use this code
```php ```php
// src/ItemsManager.php // src/ItemsManager.php
<?php <?php
namespace \Friendica; namespace Friendica;
class ItemsManager extends BaseManager { class ItemsManager extends BaseManager {
public function getAll() { ... } public function getAll() { ... }
public function getByID($id) { ... } public function getByID($id) { ... }
} }
``` ```
Even though we didn't explicitly include the `src/BaseManager.php` file, the autoloader will when this class is first defined, because it is referenced as a parent class. Even though we didn't explicitly include the `src/BaseManager.php` file, the autoloader will when this class is first defined, because it is referenced as a parent class.
It works with the "BaseManager" example here and it works when we need to call static methods: It works with the "BaseManager" example here and it works when we need to call static methods:
```php ```php
// src/Dfrn.php // src/Dfrn.php
<?php <?php
namespace \Friendica; namespace Friendica;
class Dfrn { class Dfrn {
public static function mail($item, $owner) { ... } public static function mail($item, $owner) { ... }
} }
``` ```
```php ```php
// mod/mail.php // mod/mail.php
<?php <?php
mail_post($a){ mail_post($a){
... ...
\Friendica\dfrn::mail($item, $owner); Friendica\dfrn::mail($item, $owner);
... ...
} }
``` ```
If your code is in same namespace as the class you need, you don't need to prepend it: If your code is in same namespace as the class you need, you don't need to prepend it:
```php ```php
// include/delivery.php // include/delivery.php
<?php <?php
namespace \Friendica; namespace Friendica;
// this is the same content of current include/delivery.php, // this is the same content of current include/delivery.php,
// but has been declared to be in "Friendica" namespace // but has been declared to be in "Friendica" namespace
[...] [...]
switch($contact['network']) { switch($contact['network']) {
case NETWORK_DFRN: case NETWORK_DFRN:
if ($mail) { if ($mail) {
$item['body'] = ... $item['body'] = ...
$atom = Dfrn::mail($item, $owner); $atom = Dfrn::mail($item, $owner);
} elseif ($fsuggest) { } elseif ($fsuggest) {
$atom = Dfrn::fsuggest($item, $owner); $atom = Dfrn::fsuggest($item, $owner);
q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id'])); q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
} elseif ($relocate) } elseif ($relocate)
$atom = Dfrn::relocate($owner, $uid); $atom = Dfrn::relocate($owner, $uid);
[...] [...]
``` ```
This is the current code of `include/delivery.php`, and since the code is declared to be in the "Friendica" namespace, you don't need to write it when you need to use the "Dfrn" class. This is the current code of `include/delivery.php`, and since the code is declared to be in the "Friendica" namespace, you don't need to write it when you need to use the "Dfrn" class.
But if you want to use classes from another library, you need to use the full namespace, e.g. But if you want to use classes from another library, you need to use the full namespace, e.g.
```php ```php
// src/Diaspora.php // src/Diaspora.php
<?php <?php
namespace \Friendica; namespace Friendica;
class Diaspora { class Diaspora {
public function md2bbcode() { public function md2bbcode() {
$html = \Michelf\MarkdownExtra::defaultTransform($text); $html = \Michelf\MarkdownExtra::defaultTransform($text);
}
} }
}
``` ```
if you use that class in many places of the code and you don't want to write the full path to the class every time, you can use the "use" PHP keyword if you use that class in many places of the code and you don't want to write the full path to the class every time, you can use the "use" PHP keyword
```php ```php
// src/Diaspora.php // src/Diaspora.php
<?php <?php
namespace \Friendica; namespace Friendica;
use \Michelf\MarkdownExtra; use \Michelf\MarkdownExtra;
class Diaspora { class Diaspora {
public function md2bbcode() { public function md2bbcode() {
$html = MarkdownExtra::defaultTransform($text); $html = MarkdownExtra::defaultTransform($text);
}
} }
}
``` ```
Note that namespaces are like paths in filesystem, separated by "\", with the first "\" being the global scope. Note that namespaces are like paths in filesystem, separated by "\", with the first "\" being the global scope.
You can go deeper if you want to, like: You can go deeper if you want to, like:
``` ```
// src/Network/Dfrn.php // src/Network/Dfrn.php
<?php <?php
namespace \Friendica\Network; namespace Friendica\Network;
class Dfrn { class Dfrn {
} }
``` ```
Please note that the location of the file defining the class must be placed in the appropriate sub-folders of `src` if the namespace isn't plain `\Friendica`. Please note that the location of the file defining the class must be placed in the appropriate sub-folders of `src` if the namespace isn't plain `Friendica`.
or or
``` ```
// src/Dba/Mysql // src/Dba/Mysql
<?php <?php
namespace \Friendica\Dba; namespace Friendica\Dba;
class Mysql { class Mysql {
} }
``` ```
So you can think of namespaces as folders in a Unix file system, with global scope as the root ("\"). So you can think of namespaces as folders in a Unix file system, with global scope as the root ("\").
## Related
* [Using Composer](help/Composer)
* [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)

View file

@ -90,7 +90,7 @@ function help_content(App $a) {
for($k=0;$k<$lastlevel; $k++) $toc.="</ul>"; for($k=0;$k<$lastlevel; $k++) $toc.="</ul>";
$html = implode("\n",$lines); $html = implode("\n",$lines);
$a->page['aside'] = $toc.$a->page['aside']; $a->page['aside'] = '<section class="help-aside-wrapper">' . $toc . $a->page['aside'] . '</section>';
} }
$html = " $html = "

View file

@ -98,7 +98,7 @@ blockquote {
code { code {
white-space: pre-wrap; white-space: pre-wrap;
} }
.help-content-wrapper code {display: inline} .help-content-wrapper code, .help-aside-wrapper code {display: inline}
/* /*
* standard page elements * standard page elements