From 7073f29087c684d9c949ea2d552e7ee0b31f5d0c Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Tue, 12 Mar 2013 17:20:15 +0100 Subject: [PATCH] structural changes --- .../appframework/angular.rst} | 0 .../appframework/container.rst} | 30 +++ .../{ => app}/appframework/controllers.rst | 0 .../app/appframework/database.rst | 205 ++++++++++++++ .../{ => app}/appframework/index.rst | 15 +- .../{ => app}/appframework/middleware.rst | 0 .../{ => app}/appframework/routes.rst | 0 .../{ => app}/appframework/templates.rst | 0 .../{ => app}/appframework/tutorial.rst | 0 .../{ => app}/appframework/unittesting.rst | 0 .../{ => app}/appintro/createapp.rst | 14 +- .../{ => app}/appintro/gettingstarted.rst | 0 developer_manual/{ => app}/appintro/index.rst | 0 .../{ => app}/classes/appframework/app.rst | 0 .../appframework/controller_controller.rst | 0 .../classes/appframework/core_api.rst | 0 .../appframework/db_doesnotexistexception.rst | 0 .../classes/appframework/db_mapper.rst | 0 .../db_multipleobjectsreturnedexception.rst | 0 .../dependencyinjection_dicontainer.rst | 0 .../appframework/http_downloadresponse.rst | 0 .../appframework/http_jsonresponse.rst | 0 .../appframework/http_redirectresponse.rst | 0 .../classes/appframework/http_request.rst | 0 .../classes/appframework/http_response.rst | 0 .../appframework/http_templateresponse.rst | 0 .../http_textdownloadresponse.rst | 0 .../appframework/http_textresponse.rst | 0 .../appframework/http_twigresponse.rst | 0 .../{ => app}/classes/appframework/index.rst | 0 .../appframework/middleware_middleware.rst | 0 .../middleware_middlewaredispatcher.rst | 0 .../middleware_security_securityexception.rst | 0 ...middleware_security_securitymiddleware.rst | 0 .../middleware_twig_twigmiddleware.rst | 0 .../utility_controllertestutility.rst | 0 .../utility_methodannotationreader.rst | 0 .../{ => app}/classes/core/index.rst | 0 .../{ => app}/classes/core/templates.rst | 0 .../{ => app}/classes/core/view.rst | 0 developer_manual/{ => app}/classes/index.rst | 0 developer_manual/app/contents.rst | 21 ++ developer_manual/app/css.rst | 4 +- developer_manual/app/database.rst | 251 +----------------- .../{ => app}/general/angular.rst | 0 .../{ => app}/general/codereviews.rst | 0 .../{ => app}/general/codingguidelines.rst | 0 .../{ => app}/general/debugging.rst | 0 .../{ => app}/general/dependencyinjection.rst | 0 developer_manual/{ => app}/general/index.rst | 0 developer_manual/{ => app}/general/kanban.rst | 0 .../{ => app}/general/security.rst | 0 developer_manual/app/index.rst | 53 +++- developer_manual/app/javascript.rst | 6 + developer_manual/app/schema.rst | 52 ++++ developer_manual/app/static.rst | 14 +- developer_manual/appframework/container.rst | 27 -- 57 files changed, 401 insertions(+), 291 deletions(-) rename developer_manual/{appframework/javascript.rst => app/appframework/angular.rst} (100%) rename developer_manual/{appframework/api.rst => app/appframework/container.rst} (57%) rename developer_manual/{ => app}/appframework/controllers.rst (100%) create mode 100644 developer_manual/app/appframework/database.rst rename developer_manual/{ => app}/appframework/index.rst (69%) rename developer_manual/{ => app}/appframework/middleware.rst (100%) rename developer_manual/{ => app}/appframework/routes.rst (100%) rename developer_manual/{ => app}/appframework/templates.rst (100%) rename developer_manual/{ => app}/appframework/tutorial.rst (100%) rename developer_manual/{ => app}/appframework/unittesting.rst (100%) rename developer_manual/{ => app}/appintro/createapp.rst (88%) rename developer_manual/{ => app}/appintro/gettingstarted.rst (100%) rename developer_manual/{ => app}/appintro/index.rst (100%) rename developer_manual/{ => app}/classes/appframework/app.rst (100%) rename developer_manual/{ => app}/classes/appframework/controller_controller.rst (100%) rename developer_manual/{ => app}/classes/appframework/core_api.rst (100%) rename developer_manual/{ => app}/classes/appframework/db_doesnotexistexception.rst (100%) rename developer_manual/{ => app}/classes/appframework/db_mapper.rst (100%) rename developer_manual/{ => app}/classes/appframework/db_multipleobjectsreturnedexception.rst (100%) rename developer_manual/{ => app}/classes/appframework/dependencyinjection_dicontainer.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_downloadresponse.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_jsonresponse.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_redirectresponse.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_request.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_response.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_templateresponse.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_textdownloadresponse.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_textresponse.rst (100%) rename developer_manual/{ => app}/classes/appframework/http_twigresponse.rst (100%) rename developer_manual/{ => app}/classes/appframework/index.rst (100%) rename developer_manual/{ => app}/classes/appframework/middleware_middleware.rst (100%) rename developer_manual/{ => app}/classes/appframework/middleware_middlewaredispatcher.rst (100%) rename developer_manual/{ => app}/classes/appframework/middleware_security_securityexception.rst (100%) rename developer_manual/{ => app}/classes/appframework/middleware_security_securitymiddleware.rst (100%) rename developer_manual/{ => app}/classes/appframework/middleware_twig_twigmiddleware.rst (100%) rename developer_manual/{ => app}/classes/appframework/utility_controllertestutility.rst (100%) rename developer_manual/{ => app}/classes/appframework/utility_methodannotationreader.rst (100%) rename developer_manual/{ => app}/classes/core/index.rst (100%) rename developer_manual/{ => app}/classes/core/templates.rst (100%) rename developer_manual/{ => app}/classes/core/view.rst (100%) rename developer_manual/{ => app}/classes/index.rst (100%) create mode 100644 developer_manual/app/contents.rst rename developer_manual/{ => app}/general/angular.rst (100%) rename developer_manual/{ => app}/general/codereviews.rst (100%) rename developer_manual/{ => app}/general/codingguidelines.rst (100%) rename developer_manual/{ => app}/general/debugging.rst (100%) rename developer_manual/{ => app}/general/dependencyinjection.rst (100%) rename developer_manual/{ => app}/general/index.rst (100%) rename developer_manual/{ => app}/general/kanban.rst (100%) rename developer_manual/{ => app}/general/security.rst (100%) create mode 100644 developer_manual/app/javascript.rst create mode 100644 developer_manual/app/schema.rst delete mode 100644 developer_manual/appframework/container.rst diff --git a/developer_manual/appframework/javascript.rst b/developer_manual/app/appframework/angular.rst similarity index 100% rename from developer_manual/appframework/javascript.rst rename to developer_manual/app/appframework/angular.rst diff --git a/developer_manual/appframework/api.rst b/developer_manual/app/appframework/container.rst similarity index 57% rename from developer_manual/appframework/api.rst rename to developer_manual/app/appframework/container.rst index a2e1e1e14..5e6d70d5a 100644 --- a/developer_manual/appframework/api.rst +++ b/developer_manual/app/appframework/container.rst @@ -1,3 +1,33 @@ +Runtime configuration +===================== + +.. sectionauthor:: Bernhard Posselt + + + +Dependency Injection helps you to create testable code. A good overview over how it works and what the benefits are can be seen on `Google's Clean Code Talks `_ + +The container is configured in :file:`dependencyinjection/dicontainer.php`. By default `Pimple `_ is used as dependency injection container. A `tutorial can be found here `_ + + +To add your own classes simply open the :file:`dependencyinjection/dicontainer.php` and add a line like this to the constructor: + +.. code-block:: php + + + +You can also overwrite already existing items from the App Framework simply by redefining them. + +**See also** :doc:`../general/dependencyinjection` + API abstraction layer ===================== diff --git a/developer_manual/appframework/controllers.rst b/developer_manual/app/appframework/controllers.rst similarity index 100% rename from developer_manual/appframework/controllers.rst rename to developer_manual/app/appframework/controllers.rst diff --git a/developer_manual/app/appframework/database.rst b/developer_manual/app/appframework/database.rst new file mode 100644 index 000000000..6ba38d829 --- /dev/null +++ b/developer_manual/app/appframework/database.rst @@ -0,0 +1,205 @@ +Database Access +=============== + +.. sectionauthor:: Bernhard Posselt + +Your database layer should go into the **db/** folder. It's recommended to split your data entities from your database queries. You can do that by creating a very simple PHP object with getters and setters. This object will hold your data. + +:file:`db/item.php` + +.. code-block:: php + + fromRow($fromRow); + } + } + + public function fromRow($row){ + $this->id = $row['id']; + $this->name = $row['name']; + $this->path = $row['path']; + $this->user = $row['user']; + } + + + public function getId(){ + return $this->id; + } + + public function getName(){ + return $this->name; + } + + public function getUser(){ + return $this->user; + } + + public function getPath(){ + return $this->path; + } + + + public function setId($id){ + $this->id = $id; + } + + public function setName($name){ + $this->name = $name; + } + + public function setUser($user){ + $this->user = $user; + } + + public function setPath($path){ + $this->path = $path; + } + + } + + +All database queries for that object should be put into a mapper class. This follows the `data mapper pattern `_. The mapper class could look like this (more method examples are in the **Apptemplate Advanced** app): + +:file:`db/itemmapper.php` + +.. code-block:: php + + tableName = '*PREFIX*apptemplateadvanced_items'; + } + + + /** + * Finds an item by id + * @throws DoesNotExistException if the item does not exists + * @throws MultipleObjectsReturnedException if more than one item exists + * @return Item the item + */ + public function find($id){ + $row = $this->findQuery($this->tableName, $id); + return new Item($row); + } + + + /** + * Finds an item by user id + * @param string $userId the id of the user that we want to find + * @throws DoesNotExistException if the item does not exist + * @return Item the item + */ + public function findByUserId($userId){ + $sql = 'SELECT * FROM ' . $this->tableName . ' WHERE user = ?'; + $params = array($userId); + + $result = $this->execute($sql, $params)->fetchRow(); + if($result){ + return new Item($result); + } else { + throw new DoesNotExistException('Item with user id ' . $userId . ' does not exist!'); + } + } + + + /** + * Saves an item into the database + * @param Item $item the item to be saved + * @return Item the item with the filled in id + */ + public function save($item){ + $sql = 'INSERT INTO '. $this->tableName . '(name, user, path)'. + ' VALUES(?, ?, ?)'; + + $params = array( + $item->getName(), + $item->getUser(), + $item->getPath() + ); + + $this->execute($sql, $params); + + $item->setId($this->api->getInsertId()); + return $item; + } + + + /** + * Updates an item + * @param Item $item: the item to be updated + */ + public function update($item){ + $sql = 'UPDATE '. $this->tableName . ' SET + name = ?, + user = ?, + path = ? + WHERE id = ?'; + + $params = array( + $item->getName(), + $item->getUser(), + $item->getPath(), + $item->getId() + ); + + $this->execute($sql, $params); + } + + + /** + * Deletes an item + * @param int $id the id of the item + */ + public function delete($id){ + $this->deleteQuery($this->tableName, $id); + } + + + } + +.. note:: Always use **?** to mark placeholders for arguments in SQL queries and pass the arguments as a second parameter to the execute function to prevent `SQL Injection `_ + +**DONT**: + +.. code-block:: php + + tableName . ' WHERE user = ' . $user; + $result = $this->execute($sql); + + +**DO**: + +.. code-block:: php + + tableName . ' WHERE user = ?'; + $params = array($userId); + + $result = $this->execute($sql, $params); + diff --git a/developer_manual/appframework/index.rst b/developer_manual/app/appframework/index.rst similarity index 69% rename from developer_manual/appframework/index.rst rename to developer_manual/app/appframework/index.rst index 601418a68..44204d87c 100644 --- a/developer_manual/appframework/index.rst +++ b/developer_manual/app/appframework/index.rst @@ -6,19 +6,20 @@ App Developement Using the App Framework :maxdepth: 1 tutorial + ../app/info ../app/classloader container - api routes controllers + ../app/schema database templates - javascript ../app/css + ../app/javascript + angular unittesting middleware - ../app/info - externalapi - filesystem - hooks - data-migration + ../app/externalapi + ../app/filesystem + ../app/hooks + ../app/data-migration diff --git a/developer_manual/appframework/middleware.rst b/developer_manual/app/appframework/middleware.rst similarity index 100% rename from developer_manual/appframework/middleware.rst rename to developer_manual/app/appframework/middleware.rst diff --git a/developer_manual/appframework/routes.rst b/developer_manual/app/appframework/routes.rst similarity index 100% rename from developer_manual/appframework/routes.rst rename to developer_manual/app/appframework/routes.rst diff --git a/developer_manual/appframework/templates.rst b/developer_manual/app/appframework/templates.rst similarity index 100% rename from developer_manual/appframework/templates.rst rename to developer_manual/app/appframework/templates.rst diff --git a/developer_manual/appframework/tutorial.rst b/developer_manual/app/appframework/tutorial.rst similarity index 100% rename from developer_manual/appframework/tutorial.rst rename to developer_manual/app/appframework/tutorial.rst diff --git a/developer_manual/appframework/unittesting.rst b/developer_manual/app/appframework/unittesting.rst similarity index 100% rename from developer_manual/appframework/unittesting.rst rename to developer_manual/app/appframework/unittesting.rst diff --git a/developer_manual/appintro/createapp.rst b/developer_manual/app/appintro/createapp.rst similarity index 88% rename from developer_manual/appintro/createapp.rst rename to developer_manual/app/appintro/createapp.rst index b0460d3a8..a233a457b 100644 --- a/developer_manual/appintro/createapp.rst +++ b/developer_manual/app/appintro/createapp.rst @@ -1,5 +1,5 @@ -Create An App -============= +Creating An App +=============== .. sectionauthor:: Bernhard Posselt @@ -71,8 +71,12 @@ Start coding ------------ The basic files are now in place and the app is enabled. There are two ways to create the app: -* :doc:`Use ownClouds app API ` -* :doc:`Use the App Framework app <../appframework/tutorial>` +* Use the :doc:`ownCloud app API <../app/tutorial>` +* Use the :doc:`App Framework app <../appframework/tutorial>` + +If you are new to programming and want to create an app fast you migth want to use the ownCloud app API, if you are an advanced programmer or used to frameworks you might want to use the App Framework App. + +To simplify the decision see this comparison chart: +-----------------+-------------------------+--------------------------------+ | Criteria | ownCloud app API | App Framework | @@ -85,7 +89,7 @@ The basic files are now in place and the app is enabled. There are two ways to c | | | ../general/dependencyinjection`| | | | and `TDD`_ tools | +-----------------+-------------------------+--------------------------------+ -| Maintainability | hard | medium | +| Maintainability | hard | easy | +-----------------+-------------------------+--------------------------------+ | Templates | :php:class:`OC_Template`| :php:class:`OC_Template` | | | | and `Twig`_ | diff --git a/developer_manual/appintro/gettingstarted.rst b/developer_manual/app/appintro/gettingstarted.rst similarity index 100% rename from developer_manual/appintro/gettingstarted.rst rename to developer_manual/app/appintro/gettingstarted.rst diff --git a/developer_manual/appintro/index.rst b/developer_manual/app/appintro/index.rst similarity index 100% rename from developer_manual/appintro/index.rst rename to developer_manual/app/appintro/index.rst diff --git a/developer_manual/classes/appframework/app.rst b/developer_manual/app/classes/appframework/app.rst similarity index 100% rename from developer_manual/classes/appframework/app.rst rename to developer_manual/app/classes/appframework/app.rst diff --git a/developer_manual/classes/appframework/controller_controller.rst b/developer_manual/app/classes/appframework/controller_controller.rst similarity index 100% rename from developer_manual/classes/appframework/controller_controller.rst rename to developer_manual/app/classes/appframework/controller_controller.rst diff --git a/developer_manual/classes/appframework/core_api.rst b/developer_manual/app/classes/appframework/core_api.rst similarity index 100% rename from developer_manual/classes/appframework/core_api.rst rename to developer_manual/app/classes/appframework/core_api.rst diff --git a/developer_manual/classes/appframework/db_doesnotexistexception.rst b/developer_manual/app/classes/appframework/db_doesnotexistexception.rst similarity index 100% rename from developer_manual/classes/appframework/db_doesnotexistexception.rst rename to developer_manual/app/classes/appframework/db_doesnotexistexception.rst diff --git a/developer_manual/classes/appframework/db_mapper.rst b/developer_manual/app/classes/appframework/db_mapper.rst similarity index 100% rename from developer_manual/classes/appframework/db_mapper.rst rename to developer_manual/app/classes/appframework/db_mapper.rst diff --git a/developer_manual/classes/appframework/db_multipleobjectsreturnedexception.rst b/developer_manual/app/classes/appframework/db_multipleobjectsreturnedexception.rst similarity index 100% rename from developer_manual/classes/appframework/db_multipleobjectsreturnedexception.rst rename to developer_manual/app/classes/appframework/db_multipleobjectsreturnedexception.rst diff --git a/developer_manual/classes/appframework/dependencyinjection_dicontainer.rst b/developer_manual/app/classes/appframework/dependencyinjection_dicontainer.rst similarity index 100% rename from developer_manual/classes/appframework/dependencyinjection_dicontainer.rst rename to developer_manual/app/classes/appframework/dependencyinjection_dicontainer.rst diff --git a/developer_manual/classes/appframework/http_downloadresponse.rst b/developer_manual/app/classes/appframework/http_downloadresponse.rst similarity index 100% rename from developer_manual/classes/appframework/http_downloadresponse.rst rename to developer_manual/app/classes/appframework/http_downloadresponse.rst diff --git a/developer_manual/classes/appframework/http_jsonresponse.rst b/developer_manual/app/classes/appframework/http_jsonresponse.rst similarity index 100% rename from developer_manual/classes/appframework/http_jsonresponse.rst rename to developer_manual/app/classes/appframework/http_jsonresponse.rst diff --git a/developer_manual/classes/appframework/http_redirectresponse.rst b/developer_manual/app/classes/appframework/http_redirectresponse.rst similarity index 100% rename from developer_manual/classes/appframework/http_redirectresponse.rst rename to developer_manual/app/classes/appframework/http_redirectresponse.rst diff --git a/developer_manual/classes/appframework/http_request.rst b/developer_manual/app/classes/appframework/http_request.rst similarity index 100% rename from developer_manual/classes/appframework/http_request.rst rename to developer_manual/app/classes/appframework/http_request.rst diff --git a/developer_manual/classes/appframework/http_response.rst b/developer_manual/app/classes/appframework/http_response.rst similarity index 100% rename from developer_manual/classes/appframework/http_response.rst rename to developer_manual/app/classes/appframework/http_response.rst diff --git a/developer_manual/classes/appframework/http_templateresponse.rst b/developer_manual/app/classes/appframework/http_templateresponse.rst similarity index 100% rename from developer_manual/classes/appframework/http_templateresponse.rst rename to developer_manual/app/classes/appframework/http_templateresponse.rst diff --git a/developer_manual/classes/appframework/http_textdownloadresponse.rst b/developer_manual/app/classes/appframework/http_textdownloadresponse.rst similarity index 100% rename from developer_manual/classes/appframework/http_textdownloadresponse.rst rename to developer_manual/app/classes/appframework/http_textdownloadresponse.rst diff --git a/developer_manual/classes/appframework/http_textresponse.rst b/developer_manual/app/classes/appframework/http_textresponse.rst similarity index 100% rename from developer_manual/classes/appframework/http_textresponse.rst rename to developer_manual/app/classes/appframework/http_textresponse.rst diff --git a/developer_manual/classes/appframework/http_twigresponse.rst b/developer_manual/app/classes/appframework/http_twigresponse.rst similarity index 100% rename from developer_manual/classes/appframework/http_twigresponse.rst rename to developer_manual/app/classes/appframework/http_twigresponse.rst diff --git a/developer_manual/classes/appframework/index.rst b/developer_manual/app/classes/appframework/index.rst similarity index 100% rename from developer_manual/classes/appframework/index.rst rename to developer_manual/app/classes/appframework/index.rst diff --git a/developer_manual/classes/appframework/middleware_middleware.rst b/developer_manual/app/classes/appframework/middleware_middleware.rst similarity index 100% rename from developer_manual/classes/appframework/middleware_middleware.rst rename to developer_manual/app/classes/appframework/middleware_middleware.rst diff --git a/developer_manual/classes/appframework/middleware_middlewaredispatcher.rst b/developer_manual/app/classes/appframework/middleware_middlewaredispatcher.rst similarity index 100% rename from developer_manual/classes/appframework/middleware_middlewaredispatcher.rst rename to developer_manual/app/classes/appframework/middleware_middlewaredispatcher.rst diff --git a/developer_manual/classes/appframework/middleware_security_securityexception.rst b/developer_manual/app/classes/appframework/middleware_security_securityexception.rst similarity index 100% rename from developer_manual/classes/appframework/middleware_security_securityexception.rst rename to developer_manual/app/classes/appframework/middleware_security_securityexception.rst diff --git a/developer_manual/classes/appframework/middleware_security_securitymiddleware.rst b/developer_manual/app/classes/appframework/middleware_security_securitymiddleware.rst similarity index 100% rename from developer_manual/classes/appframework/middleware_security_securitymiddleware.rst rename to developer_manual/app/classes/appframework/middleware_security_securitymiddleware.rst diff --git a/developer_manual/classes/appframework/middleware_twig_twigmiddleware.rst b/developer_manual/app/classes/appframework/middleware_twig_twigmiddleware.rst similarity index 100% rename from developer_manual/classes/appframework/middleware_twig_twigmiddleware.rst rename to developer_manual/app/classes/appframework/middleware_twig_twigmiddleware.rst diff --git a/developer_manual/classes/appframework/utility_controllertestutility.rst b/developer_manual/app/classes/appframework/utility_controllertestutility.rst similarity index 100% rename from developer_manual/classes/appframework/utility_controllertestutility.rst rename to developer_manual/app/classes/appframework/utility_controllertestutility.rst diff --git a/developer_manual/classes/appframework/utility_methodannotationreader.rst b/developer_manual/app/classes/appframework/utility_methodannotationreader.rst similarity index 100% rename from developer_manual/classes/appframework/utility_methodannotationreader.rst rename to developer_manual/app/classes/appframework/utility_methodannotationreader.rst diff --git a/developer_manual/classes/core/index.rst b/developer_manual/app/classes/core/index.rst similarity index 100% rename from developer_manual/classes/core/index.rst rename to developer_manual/app/classes/core/index.rst diff --git a/developer_manual/classes/core/templates.rst b/developer_manual/app/classes/core/templates.rst similarity index 100% rename from developer_manual/classes/core/templates.rst rename to developer_manual/app/classes/core/templates.rst diff --git a/developer_manual/classes/core/view.rst b/developer_manual/app/classes/core/view.rst similarity index 100% rename from developer_manual/classes/core/view.rst rename to developer_manual/app/classes/core/view.rst diff --git a/developer_manual/classes/index.rst b/developer_manual/app/classes/index.rst similarity index 100% rename from developer_manual/classes/index.rst rename to developer_manual/app/classes/index.rst diff --git a/developer_manual/app/contents.rst b/developer_manual/app/contents.rst new file mode 100644 index 000000000..b4b81a4b4 --- /dev/null +++ b/developer_manual/app/contents.rst @@ -0,0 +1,21 @@ +.. _contents: + +================ +App Developement +================ + +.. toctree:: + :maxdepth: 1 + + tutorial + info + classloader + schema + database + static + css + javascript + externalapi + filesystem + hooks + data-migration \ No newline at end of file diff --git a/developer_manual/app/css.rst b/developer_manual/app/css.rst index 68b195ec1..56668bf3c 100644 --- a/developer_manual/app/css.rst +++ b/developer_manual/app/css.rst @@ -1,4 +1,6 @@ CSS === -.. sectionauthor:: Bernhard Posselt \ No newline at end of file +.. sectionauthor:: Bernhard Posselt + + diff --git a/developer_manual/app/database.rst b/developer_manual/app/database.rst index ad6d7475b..409f735af 100644 --- a/developer_manual/app/database.rst +++ b/developer_manual/app/database.rst @@ -3,257 +3,30 @@ Database Access .. sectionauthor:: Bernhard Posselt -.. note:: This will likely change with the introduction of an ORM - -ownCloud uses a database abstraction layer on top of either MDB2 or PDO, depending on the availability of PDO on the server. - -Your database schema will be inside :file:`appinfo/database.xml` in MDB2's `XML scheme notation `_ where the placeholders \*dbprefix* (\*PREFIX* in your SQL) and \*dbname* can be used for the configured database table prefix and database name. - -An example database XML file would look like this: - -.. code-block:: xml - - - - *dbname* - true - false - utf8 - - *dbprefix*yourapp_items - - - id - integer - 0 - true - 1 - 4 - - - user - text - true - 64 - - - name - text - true - 100 - - - path - clob - true - - -
-
- - - -To update the tables used by the app, simply adjust the database.xml file and increase the app version number in :file:`appinfo/version` to trigger an update. - - -Your database layer should go into the **db/** folder. It's recommended to split your data entities from your database queries. You can do that by creating a very simple PHP object with getters and setters. This object will hold your data. - -:file:`db/item.php` +After the schema has been defined it is possible to query the database. ownCloud uses prepared statements. A simple query would look like this: .. code-block:: php fromRow($fromRow); - } - } - - public function fromRow($row){ - $this->id = $row['id']; - $this->name = $row['name']; - $this->path = $row['path']; - $this->user = $row['user']; - } - - - public function getId(){ - return $this->id; - } - - public function getName(){ - return $this->name; - } - - public function getUser(){ - return $this->user; - } - - public function getPath(){ - return $this->path; - } - - - public function setId($id){ - $this->id = $id; - } - - public function setName($name){ - $this->name = $name; - } - - public function setUser($user){ - $this->user = $user; - } - - public function setPath($path){ - $this->path = $path; - } + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + while($row = $result->fetchRow()) { + $userName = $row['username']; } - -All database queries for that object should be put into a mapper class. This follows the `data mapper pattern `_. The mapper class could look like this (more method examples are in the **Apptemplate Advanced** app): - -:file:`db/itemmapper.php` +If a new element is saved to the database the inserted id can be accessed by using: .. code-block:: php tableName = '*PREFIX*apptemplateadvanced_items'; - } - - - /** - * Finds an item by id - * @throws DoesNotExistException if the item does not exists - * @throws MultipleObjectsReturnedException if more than one item exists - * @return Item the item - */ - public function find($id){ - $row = $this->findQuery($this->tableName, $id); - return new Item($row); - } - - - /** - * Finds an item by user id - * @param string $userId the id of the user that we want to find - * @throws DoesNotExistException if the item does not exist - * @return Item the item - */ - public function findByUserId($userId){ - $sql = 'SELECT * FROM ' . $this->tableName . ' WHERE user = ?'; - $params = array($userId); - - $result = $this->execute($sql, $params)->fetchRow(); - if($result){ - return new Item($result); - } else { - throw new DoesNotExistException('Item with user id ' . $userId . ' does not exist!'); - } - } - - - /** - * Saves an item into the database - * @param Item $item the item to be saved - * @return Item the item with the filled in id - */ - public function save($item){ - $sql = 'INSERT INTO '. $this->tableName . '(name, user, path)'. - ' VALUES(?, ?, ?)'; - - $params = array( - $item->getName(), - $item->getUser(), - $item->getPath() - ); - - $this->execute($sql, $params); - - $item->setId($this->api->getInsertId()); - return $item; - } - - - /** - * Updates an item - * @param Item $item: the item to be updated - */ - public function update($item){ - $sql = 'UPDATE '. $this->tableName . ' SET - name = ?, - user = ?, - path = ? - WHERE id = ?'; - - $params = array( - $item->getName(), - $item->getUser(), - $item->getPath(), - $item->getId() - ); - - $this->execute($sql, $params); - } - - - /** - * Deletes an item - * @param int $id the id of the item - */ - public function delete($id){ - $this->deleteQuery($this->tableName, $id); - } - - - } - -.. note:: Always use **?** to mark placeholders for arguments in SQL queries and pass the arguments as a second parameter to the execute function to prevent `SQL Injection `_ - -**DONT**: - -.. code-block:: php - - tableName . ' WHERE user = ' . $user; - $result = $this->execute($sql); - - -**DO**: - -.. code-block:: php - - tableName . ' WHERE user = ?'; - $params = array($userId); - - $result = $this->execute($sql, $params); - -For more information about MDB2 style prepared statements, please see the `official MDB2 documentation `_ +It is also possible to use transactions. To start a transaction use ** \ No newline at end of file diff --git a/developer_manual/general/angular.rst b/developer_manual/app/general/angular.rst similarity index 100% rename from developer_manual/general/angular.rst rename to developer_manual/app/general/angular.rst diff --git a/developer_manual/general/codereviews.rst b/developer_manual/app/general/codereviews.rst similarity index 100% rename from developer_manual/general/codereviews.rst rename to developer_manual/app/general/codereviews.rst diff --git a/developer_manual/general/codingguidelines.rst b/developer_manual/app/general/codingguidelines.rst similarity index 100% rename from developer_manual/general/codingguidelines.rst rename to developer_manual/app/general/codingguidelines.rst diff --git a/developer_manual/general/debugging.rst b/developer_manual/app/general/debugging.rst similarity index 100% rename from developer_manual/general/debugging.rst rename to developer_manual/app/general/debugging.rst diff --git a/developer_manual/general/dependencyinjection.rst b/developer_manual/app/general/dependencyinjection.rst similarity index 100% rename from developer_manual/general/dependencyinjection.rst rename to developer_manual/app/general/dependencyinjection.rst diff --git a/developer_manual/general/index.rst b/developer_manual/app/general/index.rst similarity index 100% rename from developer_manual/general/index.rst rename to developer_manual/app/general/index.rst diff --git a/developer_manual/general/kanban.rst b/developer_manual/app/general/kanban.rst similarity index 100% rename from developer_manual/general/kanban.rst rename to developer_manual/app/general/kanban.rst diff --git a/developer_manual/general/security.rst b/developer_manual/app/general/security.rst similarity index 100% rename from developer_manual/general/security.rst rename to developer_manual/app/general/security.rst diff --git a/developer_manual/app/index.rst b/developer_manual/app/index.rst index 06ceead4b..fd50a9b21 100644 --- a/developer_manual/app/index.rst +++ b/developer_manual/app/index.rst @@ -1,14 +1,49 @@ +.. _index: + ================ App Developement ================ -.. toctree:: - :maxdepth: 1 +.. _Junior Jobs: http://owncloud.org/dev/junior-jobs/ - tutorial - classloader - info - externalapi - filesystem - hooks - data-migration + +.. _git crash course: http://git-scm.com/course/svn.html + +.. _Twig Templates: http://twig.sensiolabs.org/ +.. _Symfony Routing: http://symfony.com/doc/current/components/routing/introduction.html +.. _Pimple: http://pimple.sensiolabs.org/ +.. _PHPUnit: http://www.phpunit.de/manual/current/en/ + +* Take a job from our `Junior Jobs`_ + +* :doc:`general/security` +* :doc:`general/codingguidelines` +* :doc:`general/codereviews` +* :doc:`general/debugging` +* :doc:`general/kanban` +* :doc:`general/angular` | `AngularJS Documentation `_ +* :doc:`general/dependencyinjection` | `Pimple`_ + +App Developement +================ + +* :doc:`appintro/gettingstarted` +* :doc:`appintro/createapp` + +Tutorial +-------- +You can choose between the traditional and MVC style (App Framework) approach. + +* :doc:`app/tutorial` + +* :doc:`appframework/tutorial` + +App Framework +------------- + + + +API Documentation +================= +* :doc:`classes/appframework/index` +* :doc:`classes/core/index` diff --git a/developer_manual/app/javascript.rst b/developer_manual/app/javascript.rst new file mode 100644 index 000000000..4f3512761 --- /dev/null +++ b/developer_manual/app/javascript.rst @@ -0,0 +1,6 @@ +JavaScript +========== + +.. sectionauthor:: Bernhard Posselt + + diff --git a/developer_manual/app/schema.rst b/developer_manual/app/schema.rst new file mode 100644 index 000000000..c36270d44 --- /dev/null +++ b/developer_manual/app/schema.rst @@ -0,0 +1,52 @@ +Database Schema +=============== + +.. sectionauthor:: Bernhard Posselt + +ownCloud uses a database abstraction layer on top of either MDB2 or PDO, depending on the availability of PDO on the server. + +The database schema is inside :file:`appinfo/database.xml` in MDB2's `XML scheme notation `_ where the placeholders \*dbprefix* (\*PREFIX* in your SQL) and \*dbname* can be used for the configured database table prefix and database name. + +An example database XML file would look like this: + +.. code-block:: xml + + + + *dbname* + true + false + utf8 + + *dbprefix*yourapp_items + + + id + integer + 0 + true + 1 + 4 + + + user + text + true + 64 + + + name + text + true + 100 + + + path + clob + true + + +
+
+ +To update the tables used by the app, simply adjust the database.xml file and increase the app version number in :file:`appinfo/version` to trigger an update. diff --git a/developer_manual/app/static.rst b/developer_manual/app/static.rst index 4f742faa6..1171dbc06 100644 --- a/developer_manual/app/static.rst +++ b/developer_manual/app/static.rst @@ -1,9 +1,17 @@ -JavaScript and CSS -================== +Static content +============== .. sectionauthor:: Bernhard Posselt -Static content is compressed by ownCloud and therefore needs to be added in the controller. If the CSS or JavaScript does not seem to get updated, check if the debug mode is enabled. To enable it see :doc:`gettingstarted` +Static content consists of: + +* **img/**: all images +* **js/**: all JavaScript files +* **css/**: all CSS files + +.. note:: CSS and JavaScript are compressed by ownCloud so if the CSS or JavaScript do not seem to get updated, check if the debug mode is enabled. To enable it see :doc:`../appintro/gettingstarted` + + JavaScript and CSS ------------------ diff --git a/developer_manual/appframework/container.rst b/developer_manual/appframework/container.rst deleted file mode 100644 index 7ad0dabfa..000000000 --- a/developer_manual/appframework/container.rst +++ /dev/null @@ -1,27 +0,0 @@ -Dependency Injection Container -============================== - -.. sectionauthor:: Bernhard Posselt - -Dependency Injection helps you to create testable code. A good overview over how it works and what the benefits are can be seen on `Google's Clean Code Talks `_ - -The container is configured in :file:`dependencyinjection/dicontainer.php`. By default `Pimple `_ is used as dependency injection container. A `tutorial can be found here `_ - - -To add your own classes simply open the :file:`dependencyinjection/dicontainer.php` and add a line like this to the constructor: - -.. code-block:: php - - - -You can also overwrite already existing items from the App Framework simply by redefining them. - -**See also** :doc:`../general/dependencyinjection` \ No newline at end of file