protected function sortConfig(array $config)
    {
        // Sort operations
        if (isset($config['operations'])) {
            $operations = $config['operations'];
            $nodes = array();

            // Create empty dependencies array
            foreach (array_keys($operations) as $key) {
                $nodes[$key] = array(
                    'in'  => array(), // inbound dependencies
                    'out' => array(), // outbound dependencies
                );
            }

            // Populate dependencies
            foreach ($operations as $key => $operation) {
                if (isset($operation['extends'])) {
                    // This key depends on the thing being extended
                    $nodes[$key]['in'][] = $operation['extends'];

                    // The thing being extended depends on this key
                    $nodes[$operation['extends']]['out'][] = $key;
                }
            }

            // Build up $independent
            $independent = array();
            foreach ($nodes as $key => $node) {
                if (empty($node['in'])) {
                    $independent[] = $key;
                }
            }

            // While we have nodes with no inbound edges, remove it
            // from the graph and add it to the end of the list
            $sorted = array();
            while (!empty($independent)) {
                $key = array_shift($independent);
                $sorted[$key] = $operations[$key];

                // Go over this node's dependencies
                foreach ($nodes[$key]['out'] as $dependency) {
                    $nodes[$dependency]['in'] = array_diff(
                        $nodes[$dependency]['in'],
                        array($key)
                    );

                    // If the dependency has all of its inbound deps...
                    if (empty($nodes[$dependency]['in'])) {
                        // ...it's ready to be processed, add it to $independent.
                        $independent[] = $dependency;
                    }
                }

                $nodes[$key]['out'] = array();
            }

            foreach ($nodes as $node) {
                if (!empty($node['in']) or !empty($node['out'])) {
                    throw new RuntimeException(
                        "Couldn't sort graph, cycle detected!"
                    );
                }
            }

            $config['operations'] = $sorted;
        }

        return $config;
    }