使用codeigniter
Have you ever heard of Aspect Oriented Programming (AOP) before? It’s a widely used concept in developing enterprise level systems, although it hasn’t seen much use in PHP. I’m going to use this article as an opportunity to introduce PHP developers to AOP.
您以前听说过面向方面的编程(AOP)吗? 尽管在PHP中并没有太多用处,但它在开发企业级系统中被广泛使用。 我将利用本文作为向PHP开发人员介绍AOP的机会。
This tutorial will be delivered to you as a 3-part series. In this part I’ll explain the concepts of AOP. In part 2 I’ll show you the practical uses of AOP and creating a AOP rules structure. Finally, I’ll show you how to integrate AOP functionality using CodeIgniter in part 3.
本教程将分为3部分,向您提供。 在这一部分中,我将解释AOP的概念。 在第2部分中,我将向您展示AOP的实际用法以及创建AOP规则结构。 最后,在第3部分中,我将向您展示如何使用CodeIgniter集成AOP功能。
In application development we often find required functionality that needs to be used in multiple points in our code but which is not actually related to business logic. Checking authentication to make sure a user is logged in before executing a specific task is a good example. Such tasks are called cross-cutting concerns. Let’s take a look at the definition of cross-cutting concerns as given by Wikipedia:
在应用程序开发中,我们经常发现所需的功能,这些功能需要在代码中的多个点使用,但实际上与业务逻辑无关。 一个很好的例子是在执行特定任务之前检查身份验证以确保用户已登录。 这些任务称为跨领域关注点。 让我们看一下Wikipedia提供的跨领域关注点的定义:
In computer science, cross-cutting concerns are aspects of a program which affect other concerns. These concerns often cannot be cleanly decomposed from the rest of the system in both the design and implementation, and can result in either scattering (code duplication), tangling (significant dependencies between systems), or both.
在计算机科学中,横切关注点是程序中影响其他关注点的方面。 在设计和实现中,这些问题通常无法与系统的其余部分完全分解,并且可能导致分散(代码重复),纠结(系统之间的重要依存关系)或两者兼而有之。
Now that you have a basic idea of what cross-cutting concerns are, let’s see what they look like in code.
现在,您已经对跨领域关注点有了基本的了解,让我们看看它们在代码中的外观。
Consider the situation where you are the editor of a blog like SitePoint. You need to login in order to create posts, approve posts, edit post, etc. If you are not logged in, you should be directed to the login screen. In order to ensure this behavior, the code must validate authentication before each of the tasks mentioned above.
考虑一下您是像SitePoint这样的博客的编辑者的情况。 您需要登录才能创建帖子,批准帖子,编辑帖子等。如果您尚未登录,则应转到登录屏幕。 为了确保这种行为,代码必须在上述每个任务之前验证身份验证。
<?php class BlogPost extends CI_Controller { public function createPost() { if (!Authentication::checkAuthentication()) { // redirect to login } else { // proceed Messages::notifyAdmin(); } } public function approvePost() { if (!Authentication::checkAuthentication()) { // redirect to login } else { // proceed } } public function editPost() { if (!Authentication::checkAuthentication()) { // redirect to login } else { // proceed } } public function viewPost() { // ... } }Look at the above code and you can see that checkAuthentication() is called inside every method which needs the user to be logged in, and notifyAdmin() is called to notify administrators when new posts are created. Not only is this duplicated code, but the BlogPost class should be only responsible for managing posts. Authentication and notifications should be done separately. We have violated the Single Responsibility Principle:
看看上面的代码,你可以看到checkAuthentication()被调用每个这就需要先登录用户方法里面, notifyAdmin()被调用创建新的职位时通知管理员。 不仅是此重复的代码,而且BlogPost类也应仅负责管理帖子。 身份验证和通知应分别进行。 我们违反了单一责任原则 :
The single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.
单一责任原则规定,每个班级应负有单一责任,而责任应完全由班级封装。 它的所有服务都应严格地与这一责任保持一致。
So now we’ve come to a place where we can understand the meaning of AOP. Cross-cutting concerns can be grouped into objects called “aspects,” and the process of separating cross-cutting concerns from our core code is called Aspect Oriented Programming.
因此,现在我们来到一个可以理解AOP含义的地方。 跨领域关注点可以分为称为“方面”的对象,而将跨领域关注点与我们的核心代码分离的过程称为面向方面的编程。
There are few specific terms used in AOP to explain its features. Understanding these terms will likely be the key to successfully integrating AOP into your PHP projects.
AOP中几乎没有用于解释其功能的特定术语。 理解这些术语可能是将AOP成功集成到PHP项目中的关键。
Aspect 方面 Advice 忠告 Joinpoint 连接点 Pointcut 切入点We just learned what an aspect is, so now let’s see what the other three terms mean.
我们刚刚了解了一个方面是什么,所以现在让我们看看其他三个术语的含义。
The functionality of an aspect is called an advice. As the name suggests, advices define what to do in certain situations and when to do it. In our previous example, checking authentication (the what) is the advice, and it should be applied before executing code (the when) inside specific methods.
方面的功能称为建议。 顾名思义,建议定义了在某些情况下应该做什么以及何时进行。 在我们之前的示例中,检查身份验证(什么)是建议,应在执行特定方法内的代码(何时)之前应用身份验证。
Joinpoints are places in the application where we can create advices. Looking back at the sample code, you can find several places where I have called functionality that is not directly related to the business logic. In createPost(), for example, cross-cutting concerns occurs before the executing any logic with the authentication check, and afterwards with sending a message to administrators. These are both possible joinpoints.
连接点是我们可以在其中创建建议的应用程序中的位置。 回顾示例代码,您可以找到几个我称之为与业务逻辑没有直接关系的功能的地方。 例如,在createPost() ,在执行带有身份验证检查的任何逻辑之前以及随后向管理员发送消息之前,都会出现跨领域的问题。 这些都是可能的连接点。
Joinpints can be defined anywhere in your application’s code, but applying advices will only be available to certain points according to your AOP framework features and will be discussed later in this tutorial.
可以在应用程序代码中的任何位置定义Joinpints,但是根据您的AOP框架功能,应用建议仅适用于某些点,并将在本教程后面进行讨论。
Pointcut defines a way of matching advices to certain joinpoints. Though we have a couple joinpoints in our example, you could have thousands of joinpoints in your application and you might not need to apply advice to all of them. In such situations, we can match a subset of joinpoints, which is known as poincuts, and apply advices to the selected elements only.
切入点定义了一种将建议与某些联接点相匹配的方法。 尽管在示例中我们有几个联结点,但是您的应用程序中可能有成千上万个联结点,您可能不需要对所有这些都应用建议。 在这种情况下,我们可以匹配联接点的子集(称为poincuts),并将建议仅应用于选定的元素。
Assume that we want to advice createPost(), approvePost(), and editPost(), but not viewPost(). We have to somehow match those 3 methods and apply the advices. Later we will be creating an XML file with aspect details which will contain regular expressions to match joinpoints.
假设我们要建议createPost() , approvePost()和editPost() ,而不是viewPost() 。 我们必须以某种方式匹配这三种方法并应用建议。 稍后,我们将创建一个具有方面详细信息的XML文件,其中将包含与联接点匹配的正则表达式。
So to summarize, when a cross-cutting concern occurs in the application, we create an aspect that advices the application functionality in certain joinpoints selected using a pointcut.
综上所述, 当应用程序中出现横切关注点时,我们将创建一个方面,以在使用切入点选择的某些联接点中为应用程序功能提供建议。
There are few ways we can advice our code. As I mentioned earlier, the availability of these advice types depends on the type of AOP framework you use, but some types you should be familiar with are:
我们可以通过几种方法来建议我们的代码。 如前所述,这些建议类型的可用性取决于您使用的AOP框架的类型,但是您应该熟悉的一些类型是:
Before advice 咨询之前 After returning advice 返回建议后 After throwing advice 提出建议后 Around advice 围绕建议Before advice will be applied before a specific point in your code, normally a method call.
在您的代码中的特定点之前(通常是方法调用),将在建议之前应用建议。
I have used advices inside methods so far to simplify the concept and help you understand it quickly. But in the real-world, advices usually don’t come inside methods. There should be a separate global controller where each method should go in, and inside the method we can wrap the AOP functionality. This global controller is run inside the system and is not be visible to us.
到目前为止,我已经在方法内部使用了一些建议,以简化概念并帮助您快速理解它。 但是在现实世界中,建议通常不在方法内部。 每个方法应进入的单独的全局控制器,在方法内部,我们可以包装AOP功能。 该全局控制器在系统内部运行,对我们不可见。
<?php class PathController { function controlPaths($className, $funcName) { Authentication::checkAuthentication(); $classObj = new $className(); $classObj->$funcName(); } }Here I’ve created a dummy class to show you how it actually happens. Assume the controlPaths() method acts as the global entry point for the application, and each method call will be passed through this method. In the above code we have applied the advice checkAuthentication() before desired method call is actually performed. This is called before advice.
在这里,我创建了一个虚拟类来向您展示它是如何发生的。 假定controlPaths()方法充当应用程序的全局入口点,并且每个方法调用都将通过此方法传递。 在上面的代码中,我们在实际执行所需的方法调用之前应用了建议checkAuthentication() 。 这被称为先咨询。
This advice will be applied once specific functionality is completely executed and returned to the calling point. Consider the following code:
一旦特定功能完全执行并返回到调用点,则将应用此建议。 考虑以下代码:
<?php class PathController { function controlPaths($className, $funcName) { $classObj = new $className(); $classObj->$funcName(); Database::closeConnection(); } }See here that once the method is completed we clean up the database resources. This is called after returning advice.
看到这里,一旦方法完成,我们就清理数据库资源。 返回建议后将调用此方法。
If the function throws an exception in the execution process, After throwing advice can be be applied. Here, the after throwing advice is the error reporting.
如果函数在执行过程中引发异常,则可以应用“引发后建议”。 在这里,抛出错误的建议就是错误报告。
<?php class PathController { function controlPaths($className, $funcName) { try { $classObj = new $className(); $classObj->$funcName(); } catch (Exception $e) { Error::reportError(); } } }A fourth type of advice is around advice, which is a combination of both before advice and after returning advice.
第四类建议是围绕建议的,这是建议之前和返回之后两者的结合。
<?php class PathController { function controlPaths($className, $funcName) { Logger::startLog(); $classObj = new $className(); $classObj->$funcName(); Logger::endLog(); } }In this article I introduced you to AOP. You should have a basic understanding of what AOP tries to accomplish, and be familiar with some of the basic AOP terms. In the next part of this series I’ll show you where AOP is needed in real world projects and how we can use CodeIgniter hooks to create AOP functionality. Stay tuned!
在本文中,我向您介绍了AOP。 您应该对AOP试图完成的工作有基本的了解,并熟悉一些基本的AOP术语。 在本系列的下一部分中,我将向您展示实际项目中需要AOP的位置,以及如何使用CodeIgniter挂钩创建AOP功能。 敬请关注!
Image via Fotolia
图片来自Fotolia
翻译自: https://www.sitepoint.com/explore-aspect-oriented-programming-with-codeigniter-1/
使用codeigniter
相关资源:jdk-8u281-windows-x64.exe