Entries tagged as xmlRelated tags php ant book bug fix changes checkstyle company conference consulting continuous integration crazy cruisecontrol css design quality dortmund elephpant ext/filter ezcomponents fabien potencier fun hamburg lint logger npath complexity php5 phpbbq phpmd phpt phpug phpugdo phpundercontrol phpunit php_codesniffer php_depend pirum pmd quality quality assurance release announcement software architecture software design software metrics static code analysis static_reflection support talks testing thinkpad tools training unconference unit test xpath zce zend certified engineer overview pyramid xslMonday, September 8. 2008XSL Hell :-)
While traveling from Cologne to Schwelm I was working on a xsl stylesheet that produces odd/even rows for a list view. Oh, you say: "Not really hard to implement! Why do you blog about such stuff?" Either I am totally blind or the odd/even topic is really hard to solve problem for recursive, tree like structures, so please: "repeat this answer/question for the following xsl fragment"
Saturday, August 4. 2007Exploring the MVCDuring the last weeks while I have read some documents about the model view controller pattern (MVC), which seem to be wrong in my eyes or better expressed, which mix and merge different application layers in a wrong way, I decided to write this blog post. Everybody who reads this text should know for what the shortcut MVC stands. It describes the responsibilities and dependencies between model, view and logic in the top level layer of an application. As I understand it, this means all parts in the presentation layer and nothing else. But most documents wrote about business logic, when the topic controller is treated, and this assertion is wrong in my opinion. Business logic and business models aren't part of the presentation layer, they build a further layer in an application. Keeping each layer independent, means that the presentation should know as few as possible about the underlying business logic. Because practice is more understandable than all written text, I would like to explain my thoughts with a little example. The following diagram shows a simple business modell for a product catalog which implements a transaction based product storing. Beside the business classes you can find all possible exceptions in this diagram. The class To insert a new At this point I will ignore the details and the whole environment around these two components and I will concentrate onto the method Now let's take a look at the required logic for an insert operation. What must be done? First we have to get an instance of <?php
public function doAddProduct() { $name = $this->view->getText(); // Create a new product $product = new mpProduct( $name ); // Get the product catalog $catalog = mpProductCatalog::getInstance(); // Create a new transaction $transaction = $catalog->createTransaction( $product ); try { // Start transaction $transaction->begin(); // Add new product to catalog $catalog->addProduct( $product ); // Commit product transaction $transaction->commit(); // Reset input $this->view->setText( "" ); } catch ( mpProductInTransactionException $e ) { // Show error message $this->showDialog( "Transaction Error", $e->getMessage() ); } catch ( mpProductExistsException $e ) { try { // Rollback product transaction $transaction->rollback(); // Show error message $this->showDialog( "Name Error", $e->getMessage() ); } catch ( mpProductTransactionNotOpenException $e ) { // Show error message $this->showDialog( "Transaction Error", $e->getMessage() ); } } } ?> One weak point this example shows very well is that the controller has at least six dependencies to the business model. Since I assume that dependencies between a business model and the controller will occur more than one time in an application, which makes the maintenance of the business model source code very difficult. Another point that can happen is the need of another presentation layer for your complex business model. For web applications this requirement should be resolvable by the choice of a suitable frameworks, but in some situations it's conceivable that is not possible without larger expenditure. So I created a second GTK based presentation layer for the business model. The source code for this gtk frontend can be found here controller, view. If we compare both So! What can be done to solve this problem? The simple answer to this question is, we have to decouple both layers. So we have to implement a simplified model controller(MC) that encapsulates the business model from the MVC. This technology is also well known under the terms Facade or Command. A very good description can be found in the Java J2EE Blueprints. The following listing shows the new domain/business facade. <?php class mpDomainFacade { public function addProduct( $productName ) { // Create a new product $product = new mpProduct( $productName ); // Get the product catalog $catalog = mpProductCatalog::getInstance(); // Create a new transaction $transaction = $catalog->createTransaction( $product ); try { // Start transaction $transaction->begin(); // Add new product to catalog $catalog->addProduct( $product ); // Commit product transaction $transaction->commit(); } catch ( mpProductInTransactionException $e ) { throw new mpDomainException( "Transaction Error", $e->getMessage() ); } catch ( mpProductExistsException $e ) { try { // Rollback product transaction $transaction->rollback(); // Show error message throw new mpDomainException( "Name Error", $e->getMessage() ); } catch ( mpProductTransactionNotOpenException $e ) { // Show error message throw new mpDomainException( "Transaction Error", $e->getMessage() ); } } } } class mpDomainException extends RuntimeException { private $title = ""; public function __construct( $title, $message ) { parent::__construct( $message ); $this->title = $title; } public function getTitle() { return $this->title; } } ?> As you can see this new subsystem encapsulates the whole insert operation. Everything a client has to do is to pass the new product name into the component and the client must care about thrown <?php public function doAddProduct() { $name = $this->view->getText(); // Create a domain Facade $facade = new mpDomainFacade(); try { // Try to add product $facade->addProduct( $name ); // Reset input $this->view->setText( "" ); } catch ( mpDomainException $e ) { $this->showDialog( $e->getTitle(), $e->getMessage() ); } } ?> The produced overhead is really limited, because the new facade uses same method calls as the original implementation. Even if the introduction of own Value-Objects for communication between Controller and Facade, which weren't used in the shown example, would result in an additional overhead, I have the opinion that the presented technology is important for all large and long running application. The modified source code for the controllers and the facade can be found here: facade, web controller, gtk controller. Since there are a many great applications that aren't working with the presented separation and that are very successful, I assume, that some of my readers will not agree with me. It is also possible that the shown technology is more java and not the php way of doing things. But when I was young, I smoked to much java And now I invite you to discuss with me and I close this post with a citation from Arne Nordmann:
Tuesday, July 3. 2007xpathingHave you ever worked with external xml documents where you don't know the hole structure or exactly all the used namespaces? It's a pain. Such a situation occured for one of our user group members, when he had to process a complex rdf document with multiple different xml markups. The following listing shows a modified and simplified version of this rdf document <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="resource" xmlns:br="http://binary.resource/"> <br:length />1024</br:length> </rdf:Description> <rdf:Description rdf:about="meta" xmlns:bm="http://binary.meta/"> <bm:author>Manuel Pichler</bm:author> <bm:published>2007-07-03</bm:published> </rdf:Description> <rdf:Description rdf:about="more" xmlns:bmore="http://binary.more/"> <bmore:more>more and more and more namespaces</bmore:more> </rdf:Description> </rdf:RDF> How would you handle such a structure where you know all or almost all possible markups, but each is optional? The design for the Document Object Model (DOM) assumes that you know all used namespaces in a document. Ok you can use <?php
$dom = new DOMDocument(); $dom->load( "xpath.xml" ); $foundNS = {FNAMEL}">array(); $nodes = $dom->getElementsByTagNameNS( "*", "*" ); foreach ( $nodes as $node ) { if ( $node->namespaceURI === "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ) { continue; } if ( {FNAMEL}">isset( $foundNS[$node->namespaceURI ] ) ) { continue; } $foundNS[$node->namespaceURI] = true; {FNAMEL}">print "ns-prefix: {$node->prefix}, ns-uri: {$node->namespaceURI}\n"; } ?> This could be a good solution for small documents, but consider really large documents with thousands of elements where each element is part of a namespace. This solution would load all these nodes into user land code and check there for an unknown/unvisited namespace which will be very expensive. So we have to find another solution for our problem. And here comes XPath into the play. XPath provides a large set of functions and constructs that help solving this problem. <?php
$dom = new DOMDocument(); $dom->load( "xpath.xml" ); $xp = new DOMXPath( $dom ); $xp->registerNamespace( "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ); $nodes = $xp->evaluate( "//*[ name(.) = 'rdf:Description' ]/*[ namespace-uri(.) != namespace-uri(..) and namespace-uri(.) != '' and namespace-uri(.) != namespace-uri(preceding-sibling::*)]" ); foreach ( $nodes as $node ) { {FNAMEL}">print "ns-prefix: {$node->prefix}, ns-uri: {$node->namespaceURI}\n"; } ?> What we do here is. We look for the Comments and optimizations are very welcome.
(Page 1 of 1, totaling 3 entries)
|
ProjectsFurther stuffCategories |

