dimanche 10 mars 2019

Doctrine DBAL Visitor pattern

DBAL in Doctrine has implementation of Visitor pattern. There are an interface with a few accept-methods.

interface Visitor
{
    /**
     * @return void
     */
    public function acceptSchema(Schema $schema);

    /**
     * @return void
     */
    public function acceptTable(Table $table);

    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
    }
}

And some classes like

class Schema
{
    /**
     * @return void
     */
    public function visit(Visitor $visitor)
    {
        $visitor->acceptSchema($this);

        if ($visitor instanceof NamespaceVisitor) {
            foreach ($this->namespaces as $namespace) {
                $visitor->acceptNamespace($namespace);
            }
        }

        foreach ($this->_tables as $table) {
            $table->visit($visitor);
        }

        foreach ($this->_sequences as $sequence) {
            $sequence->visit($visitor);
        }
    } 
}

Why method Schema::visit has parameter named $visitor ? All definitions of Visitor tells that exactly Visitor should have method visit. So accept method should be inside of Visitable So is there are some naming issues with Schema::visit($visitor)? Is DBAL\Visitor in fact Visitable? If this is true, why Visitor::accept does not call $schema->visit($this) ?

Some implementation of Dbal\Visitor looks like

  /**
     * {@inheritdoc}
     */
    public function acceptSequence(Sequence $sequence)
    {
        $this->sequences->attach($sequence);
    }

Is it not necessary to call $visitor->visit($this) inside of Visitable::accept()?

Aucun commentaire:

Enregistrer un commentaire