PHP - Dependency Class

I'm using the Dependency Class from this question PHP dependency class i'm using in particular the version from Paul Norman, it works almost perfectly, but i found out that the order doesn't always come out correclty

You can check the code here http://codepad.org/EBtWWn1b

The result is wrong, it should be

Loaded: Array
(
    [0] => menus
    [1] => settings
    [2] => countries
    [3] => dashboard
    [4] => languages
    [5] => modules
    [6] => users
)
Failed: Array
(
)

Do you know any way to fix it ?

Thanks !

Answers


I don't know what to tell you about Paul Norman's solution, but there seems to be a bug... I tried my hand at a solution, it seems to satisfy the depencies in your example (although doesn't list them in exactly the order you wanted...)

class Dependencies
{
    private $items;
    private $depends;
    private $hasDependency;

    public function __construct()
    {
        $this->items = array();
        $this->depends = array();
        $this->hasDependency = array();
    }

    public function add($item, $dependsOn = array())
    {
        $this->items[] = $item;
        foreach ($dependsOn as $dependsOnItem)
        {
            $this->items[] = $dependsOnItem;
            $this->depends[$dependsOnItem][] = $item;
        }

        $this->items = array_unique($this->items);
        $this->hasDependency[$item] = $dependsOn;
    }

    public function getLoadOrder()
    {
        $order = array();

        $hasChanged = true;
        while (count($order) < count($this->items) && $hasChanged === true)
        {
            $hasChanged = false;
            foreach ($this->hasDependency as $item => $dependencies)
            {
                if ($this->satisfied($item, $order))
                {
                    $order[] = $item;
                    unset($this->hasDependency[$item]);
                    $hasChanged = true;
                }
            }
        }

        if (count($order) < count($this->items) && $hasChanged === false)
        {
            echo 'Impossible set of dependencies';
            return false;
        }

        return $order;
    }

    private function satisfied($item, $addedSoFar)
    {
        $dependencies = $this->hasDependency[$item];

        foreach ($dependencies as $dependency)
        {
            if (!in_array($dependency, $addedSoFar))
            {
                return false;
            }
        }

        return true;
    }
}

$deps = new Dependencies();

$deps->add('countries', array('menus', 'settings'));
$deps->add('dashboard', array('menus'));
$deps->add('languages', array('menus', 'settings'));
$deps->add('menus');
$deps->add('modules'  , array('menus'));
$deps->add('settings' , array('menus'));
$deps->add('users'    , array('menus'));

print_r($deps->getLoadOrder());

Need Your Help

Get GPS data from an image Java code

java image metadata jpeg

I would like to get the metadata from an image file in my local system using Java code

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.