ownCloud 8 for Application Developers

In the development update over December, a great many pull requests relevant for developers were described, including the fact that PHP 5.4 is required for ownCloud Server 8.0, the brand new dependency management for apps with the ‘Intelligent container’ that can figure out dependencies by itself and many extensions and improvements to the API, as well as some deprecations. See more articles about what’s coming for users and system administrators.

The ownCloud development tool, a Python 3 tool to build new and improve existing ownCloud apps, has been improved significantly to allow developers to access and benefit from these improvements. Moreover, the ownCloud 8 app tutorial now includes best practices on how to build your own first app. To kickstart this first app, there is a brand new tutorial app on github. Let’s see how this all fits together!

Getting started

Starting with the app development documentation, you get the instruction to first set up a Development Environment, after which you should read the Security Guidelines and the Coding Style & General Guidelines. Then it is time to start with the Tutorial itself. The tutorial is about about building a small notes app – the result of which is the new tutorial app on github!

You can create a base app with the ocdev generator tool, it supports dependency injection, routing, a database layer, middleware and controllers – and all the features of ownCloud 8. In order to create the right layout you need to install or update ocdev. If you have ocdev installed run:

pip install --update --pre ocdev

otherwise run

pip install --pre ocdev

From here, the ownCloud tutorial continues with the generation of the base app:

ocdev setup core --dir owncloud  --branch stable8

note that the stable8 branch will appear once ownCloud 8 is out – for now, use master!
First you want to enable debug mode to get proper error messages. To do that add DEFINE(‘DEBUG’, true); at the end of the owncloud/config/config.php file:

echo "\nDEFINE('DEBUG', true);" >> owncloud/config/config.php

Now open another terminal window and start the development server:

cd owncloud
php -S localhost:8080

Afterwards the app can be created in the apps folder:

cd apps
ocdev startapp OwnNotes

From here on, you can continue with the ownCloud tutorial and build a basic app!
Note: the current ownCloud Notes app is looking for a maintainer!

What is new and improved

Besides the improved documentation and tools, ownCloud brings technical improvements which make the life of app developers easier. The most important of those are app dependencies and the automatic container assembly.

app dependencies

they are not installed but only checked, like if libxml version is < 2.7.9. This lets apps like News check if the user runs php 5.4 rather than have an incompatible app break the install when running a lower php version.

automatic container assembly

If you are reading through the tutorial you can see that the NoteController has the constructor:

public function __construct($AppName, IRequest $request){

By default when the request comes in and it is matched against note#index it tries, if not configured explicitly, to instantiate the controller by using reflection on its constructor parameters. If there is a type hint for a parameter it tries to create the annotated class again by reflection etc. If there is no type hint it looks up the value in the container. Sounds very complicated, but is very easy to use. The documentation explaining this is here.

In short: we now have a very easy and lazy way to write good code. It is in fact easier and faster now to use the App Framework than to write code in the old fashioned and very hard to test way. It’s also fast. According to measurements by Raydiation it amounts to just 1/1000 additional time of normal request.
You don’t really need to configure the container anymore, most of it can be done automatically by using reflection. This improvement in default behavior means that in most cases, developers won’t need to deal with class instantiation manually. Of course, it is still possible to overwrite things by hand, if needed. As code often speaks better than words, here are two examples from ownCloud 7 and ownCloud 8.

owncloud 7

Let’s say appinfo/application.php contains:

namespace OCA\MyApp\AppInfo;

use OCP\AppFramework\App;

use OCA\MyApp\Controller\AuthorController;

class Application extends App {
   /**
    * Define your dependencies in here
    */
   public function __construct(array $urlParams=array()){
     parent::__construct('myapp', $urlParams);

     $container = $this->getContainer();

     /**
      * Controllers
      */
     $container->registerService('AuthorController', function($c){
       return new AuthorController(
         $c->query('AppName'),
         $c->query('Request'),
         $c->query('ServerContainer')->getURLGenerator()
       );
     });
   }
}

Then controller/authorcontroller.php contains:

namespace OCA\MyApp\Controller;

use OCP\AppFramework\Controller;
use OCP\IRequest;
use OCP\IURLGenerator;

class AuthorController extends Controller {

     public function __construct($AppName, IRequest $request, 
IURLGenerator $generator) {
         parent::__construct($AppName, $request);
     }
}

ownCloud 8

For ownCloud 8, you can skip the container, and just have controller/authorcontroller.php with the code and thigs:

namespace OCA\MyApp\Controller;

use OCP\AppFramework\Controller;
use OCP\IRequest;
use OCP\IURLGenerator;

class AuthorController extends Controller {

     public function __construct($AppName, IRequest $request, 
IURLGenerator $generator) {
         parent::__construct($AppName, $request);
     }
}

Much more

With the improvements to documentation and app features like dependency handling, writing ownCloud apps has been simplified a lot. At the same time, the improvements to the ownCloud API like the interface for the root folder and a simple plugin system for Javascript, gives app developers much more functionality at their fingertips. A few more new features and improvements:

  • Various other tags for info.xml have been added that are related to the app store
  • Routes received the defaults parameter to set route variable defaults when not given
  • Routes received the postfix parameter to allow multiple urls pointing at the same resource
  • Menu buttons can now be added easily for navigation entries (already used in the News app)
  • OCP\AppFramework\DataResponse can now be used to wrap data and set Http error codes when using responders
  • Navigation entry undo and edit styles have been added
  • OCP\AppFramework\HttpResponse now supports setting cookies
  • A container is now optional
  • vendor_script and vendor_style template functions have been added to load styles and scripts from your vendor folder

A much more complete changelog of everything impacting developers in ownCloud 8 can be found here.

For developers of applications which want to connect to ownCloud, the external API has also been improved and further defined.

If you like to keep an eye on ownCloud development, consider reading our regular development reports.

Testing

ownCloud is currently in testing phase and if you’re interested in helping out consult our testing documentation. You can find daily builds of the upcoming ownCloud Server 8.0 release on the bottom of our download page.

Most of this article was contributed by Raydiation – who also wrote much of the resources for app developers!

2 Responses to “ownCloud 8 for Application Developers”

  1. Charles

    I want to update my ownCloud from version 7 to version 8, and I would like to know after I update the data I had in version 7 will be still there or I will lost the data I had before.

    • Jos

      It will be still there (provided nothing breaks, of course).

Comments are closed.