def get_all_layers(layer):
    """
    This function gathers all layers below a given :class:`Layer` instance,
    including the layer itself.

    :usage:
        >>> l_in = nntools.layers.InputLayer((100, 20))
        >>> l1 = nntools.layers.DenseLayer(l_in, num_units=50)
        >>> all_layers = nntools.layers.get_all_layers(l1)
        >>> # all_layers is now [l1, l_in]

    :parameters:
        - layer : Layer
            the :class:`Layer` instance for which to gather all layers feeding
            into it.

    :returns:
        - layers : list
            a list of :class:`Layer` instances feeding into the given instance.
    """
    layers = [layer]
    layers_to_expand = [layer]
    while len(layers_to_expand) > 0:
        current_layer = layers_to_expand.pop(0)
        children = []

        if hasattr(current_layer, 'input_layers'):
            children = current_layer.input_layers
        elif hasattr(current_layer, 'input_layer'):
            children = [current_layer.input_layer]

        # filter the layers that have already been visited.
        children = [child for child in children if child not in layers]
        layers_to_expand.extend(children)
        layers.extend(children)

    return layers